craft
craft

Reputation: 2135

Why do I need to specify the reuseIdentifier in a UITableViewCell?

I know how to set up custom UITableViews with custom cells, but I don't understand why I need to set the class AND the reuseIdentifier. And because of that, it often leads to scenarios where I am simply naming the reuse identifier with the same name as the UITableview cell class.

As a pratical example:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

  let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath) as? HeaderCell

  return cell!
}

I am trying to understand why the reuse identifier is even necessary. Is there a scenario where I would use the same class, but have two different reuse identifier names?

Upvotes: 1

Views: 279

Answers (1)

Rob Napier
Rob Napier

Reputation: 299265

Certainly. It's completely legal to have a UITableViewCell that's just a UITableViewCell (not a subclass). And you might configure them different ways, and have them all in the same table. Or your HeaderCell might be a "MyCustomStyleCell" that you use in different places, and just configure it for the header, rather than making a HeaderCell subclass. There's no rule that you have to make a subclass for each kind of cell (it's not even particularly encouraged by UIKit).

Upvotes: 2

Related Questions