So far, we have seen that a table view is responsible for building itself and we need to provide a very minimum setup for a table view.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "This is the cell for \(indexPath.row)"
return cell
}
We wrote these methods, but table view itself takes care of calling them.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
To work with reusable cells in a table view doesn't require us to add any new methods, it's just going to be a small change to the regular table view cellForRowAt indexPath method.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "simpleCell", for: indexPath)
cell.textLabel?.text = "This is the cell for \(indexPath.row)"
return cell
}
So instead of always instantiating a new cell, we will call tableView.dequeueReusableCell
method to dequeue a reusable cell so that we can change its contents and return it.
If there isn't a reusable cell available to dequeue, this method is smart enough just to create a new cell object. This method needs two arguments;
Now we need one minor thing by creating the identifier of "simpleCell" which we have passed as a first argument to the tableView.dequeueReusableCell
method.
So Let's open the Main.storyboard, search for Table View Cell in Object Library and drag it to the storyboard.
Now when you run your application, you will see the same result, but you get smoother scrolling and much more efficient use of memory.
You can use different styling for your table view cells than just having a single line of text. There are four different built-in styles for table view cells.
You can define your custom cells and their contents using Xcode to design them and dragging on controls from the Object library.
Now let's use these properties like images, detailTextLabel and accessory types on our table view cells. First, let's add some image/icon to your project in an asset catalog file by selecting the assets.xcassets file.
At the bottom of Document Outline, you will see a plus button to add a new asset to this file.
Select a New Image Set, call it a clock and drag an image you want to show in the imageView. Go back to the controller class and add the following code to add imageView, detailTextLabel, and accessoryType.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "simpleCell", for: indexPath)
cell.imageView?.image = UIImage(named: "clock")
cell.detailTextLabel?.text = "This is a subtitle."
cell.accessoryType = .disclosureIndicator
cell.textLabel?.text = "This is the cell for \(indexPath.row)"
return cell
}
Now let's run your application.