Search code examples
iosobjective-cuitableviewuisearchbaruisearchdisplaycontroller

Search Bar And Search Bar Controller Crashes By Just Tapping On It


Ok this is the first time i am implementing a search bar. I have followed a couple of tutorials and started implementing it inside my project that contains a tableviewcontroller. Inside my Storyboard, I dragged and added a "Search Bar And Search Bar Controlller" above the custom tableview cell.

By default, if you just drag it inside your tableview controller and do nothing and just run the program and tap on the search bar, it won't do anything as there has been no implementation done.

But in my case, just by tapping on it, the program crash and shows the following:

2013-09-24 05:29:32.468 TechY[4189:a0b] *** Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:6235
2013-09-24 05:29:32.533 TechY[4189:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

So i am stopped here and can't do rest of the implementation. Can somebody point out what could be the problem? Thanks!


Solution

  • Well you need to take a look at your UITableViewDataSource's tableView:cellForRowAtIndexPath: and make sure you are returning a UITableViewCell. Most likely it is because your table view returns one when you run dequeueReusableCellWithIdentifier: but your UISearchDisplayController's tableView does not.

    I end up running something like this:

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil && tableView != self.tableView) {
        cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }