Chap16 List Default Identifier for UItableViewCell

func makeCell(for tableView: UITableView) -> UITableViewCell {
let cellIndentifier = "Cell"
if let cell = tableView.dequeueReusableCell(withIdentifier: cellIndentifier) {
  return cell
} else {
  return UITableViewCell(style: .default, reuseIdentifier: cellIndentifier)
}

When i just create tableView. “Cell” is default identifier for UItableViewCell. Right or Wrong???

I don’t believe the Apple Documentation mentions what the default reuse identifier for a cell is. I have seen others use “cell” in code but I would advice against doing this since this is not documented behaviour. Should Apple change this at some point in time, your code would break.

Instead, first register a reuse identifier in your view controller using code similar to the following:

tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "myCell")

And then use that identifier in your code to dequeue cells.

Did you register identifier “Cell” before write makeCell() function in your code???

Sorry, I didn’t realize you were asking a question about code from the book :slight_smile: I thought it was a generic question.

The code in the book works like this - it will try to dequeue a cell first, but it would fail. Then the else branch of the condition creates a new cell with the identifier provided. That way, you don’t have to register the cell identifier beforehand.

I believe it was done this way to illustrate the different ways you can create a new cell. However, when you’re normally creating table cells and don’t have a prototype cell in your storyboard, simply registering the cell identifier beforehand would result in more straightforward and cleaner code :slight_smile:

This topic was automatically closed after 166 days. New replies are no longer allowed.