Let’s get your custom cells onscreen. In ItemsViewController’s tableView(_:cellForRowAt:) method, you will dequeue an instance of ItemCell for every row in the table.
Now that you are using a custom UITableViewCell subclass, the table view needs to know how tall each row is. There are a few ways to accomplish this, but the simplest way is to set the rowHeight property of the table view to a constant value. You will see another way later in this chapter.
Open ItemsViewController.swift and update viewDidLoad() to set the height of the table view cells.
override func viewDidLoad() { super.viewDidLoad() // Get the height of the status bar let statusBarHeight = UIApplication.shared.statusBarFrame.height let insets = UIEdgeInsets(top: statusBarHeight, left: 0, bottom: 0, right: 0) tableView.contentInset = insets tableView.scrollIndicatorInsets = insets tableView.rowHeight = 65 }
Now that you have registered the ItemCell with the table view (using the prototype cells in the storyboard), you can ask the table view to dequeue a cell with the identifier “ItemCell.”
In ItemsViewController.swift, modify tableView(_:cellForRowAt:).
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell { // Get a new or recycled celllet cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell",for: indexPath)let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell // Set the text on the cell with the description of the item // that is at the nth index of items, where n = row this cell // will appear in on the tableview let item = itemStore.allItems[indexPath.row]cell.textLabel?.text = item.namecell.detailTextLabel?.text = "$(item.valueInDollars)"// Configure the cell with the Item cell.nameLabel.text = item.name cell.serialNumberLabel.text = item.serialNumber cell.valueLabel.text = "$(item.valueInDollars)" return cell }
First, the reuse identifier is updated to reflect your new subclass. The code at the end of this method is fairly obvious – for each label on the cell, set its text to some property from the appropriate Item.
Build and run the application. The new cells now load with their labels populated with the values from each Item.