0

I have a tableViewController with a search bar and a search display controller.

That main tableViewController's tableView has a prototype cell defined on storyboard.

I am having crashes on the line

   FileManagerTableViewCell *cell = (FileManagerTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

when the search tableview is about to be displayed. Apparently the search tableView cannot access the prototype cells defined on the main tableView on storyboard.

How do I make the search tableview access the prototype cell defined for the main tableview on storyboard?

Duck
  • 34,902
  • 47
  • 248
  • 470

2 Answers2

0

If you want both tables to use the same cell, you should design that cell in a xib file instead of in the storyboard. Have both tables register the nib, using registerNib:forCellReuseIdentifier:.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • thanks, but I have discovered how to do that and what was the problem. See my answer. Thanks anyway. – Duck Oct 10 '14 at 21:23
0

The answer to this is simple to edit but complex to understand why Apple created something half-cooked like that. The solution comes from the first comment of this answer

The answer is to use self.tableview instead of tableview.

   FileManagerTableViewCell *cell = (FileManagerTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];

The explanation: when a search starts, iOS switches to show the search tableview but that tableView has no prototype cells, so this line

   FileManagerTableViewCell *cell = (FileManagerTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

is asking iOS to dequeue a cell from the search tableView. By using self.tableview, you are saying to dequeue a cell from the main tableView, that contains a prototype cell defined on storyboard.

I hope one day iOS APIs are designed to make our lives easy, thing that is far from happening today.

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
Duck
  • 34,902
  • 47
  • 248
  • 470