Tutorial 4 - Locations: tableView willDisplayCell doesn't format labels


I’m at a loss as to why this function in LocationDetailsController works in the final sample code but fails for me (I added the “print” statements in my version, as well as the checks for nil in the label text):

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.backgroundColor = UIColor.black


if let textLabel = cell.textLabel {
  if let text = textLabel.text {
    print("textLabel.text is \(text)")
  textLabel.textColor = UIColor.white
  textLabel.highlightedTextColor = textLabel.textColor

if let detailLabel = cell.detailTextLabel {
  detailLabel.textColor = UIColor(white: 1.0, alpha: 0.4)
  detailLabel.highlightedTextColor = detailLabel.textColor

let selectionView = UIView(frame: CGRect.zero)
selectionView.backgroundColor = UIColor(white: 1.0, alpha: 0.2)
cell.selectedBackgroundView = selectionView

In my code, both cell.detailTextLabel.text and cell.textLabel.text are nil in every row in the table at runtime. That makes sense to me since we hooked up all those labels that change to IBOutlet instance variables up at the top of the class, and set those instance variables to the appropriate values. Likewise the textLabel equivalents are labels that we dragged onto the storyboard, rather than setting them programmatically in viewDidLoad or somewhere.

In the equivalent place in the sample code I added print statements and see “Category”, “Description”, etc printed out in the debug area as the cells are displayed, so clearly the sample code knows that the configured static values correspond to UITableViewCell’s built-in labels

I compared every settable property I could find in main.storyboard for these UILabels with my project and the final project, and I can find no difference. Likewise with the code. Is there some magic that happens somewhere I’m missing to tell the storyboard that our instance variable labels/static labels correspond to the built-in UITableViewCell “textLabel”?

Some of the cells in this table view should have the “detail right” style. Such cells have a textLabel and a detailTextLabel, so for those cells these properties should not be nil.

If you dropped your own UILabels into these cells, then you did not follow the instructions from the tutorial. :wink:

In the tutorial, we use the built-in styles wherever we can – because it saves a bunch of work – and only use our own UILabels where the built-in styles are not suitable.

Does this answer your question?

Ah… I see the error I made now. I tried the “build it on my own” exercise then later switched the cells to detail right as instructed, but didn’t catch that I had incorrectly dragged in my own labels rather than just editing the “default” labels provided by the “Detail Right” type of cell on the storyboard. I’m not sure how I missed that :-/. Thanks for the tip.