Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios 5 UISearchDisplayController crash

Tags:

I am implementing a UITableView with UISearchDisplayController in xcode 4.2. UITableView & UISearchDisplayController are created in StoryBoard. I set the Cell Identifier (SampleCell) for UITableView and access it like

cell = [tableView dequeueReusableCellWithIdentifier:@"SampleCell"]; 

UItableView is working fine. But once i try to search, the app crash with below error.

*** Assertion failure in -[UISearchResultsTableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:6072 2011-11-09 22:22:16.058 SampleApp[14362:fb03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' 

I guess I need to set the cell identifier for self.searchDisplayController.searchResultsTableView cell. But I don't know how. Thanks in advance for any help. =)

like image 508
moon Avatar asked Nov 09 '11 14:11

moon


2 Answers

Use [self.tableView dequeue...], not [tableView dequeue...].

The cell you're trying to dequeue is linked to your primary view controller's tableView in the storyboard, NOT the searchDisplayController's newly created tableView (which has no cell identifiers linked to it). If you just message "tableView" then your dequeue message goes to the searchDisplayController's tableView since that's what was passed into the cellForRowAtIndexPath:... method.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"CellId"];      // do your thing      return cell; } 
like image 164
hustoj2 Avatar answered Oct 10 '22 14:10

hustoj2


Hard bug to track indeed, it seems that every time you do a search a new tableview is created. Meaning that your cell registering has to be taken out of ViewDidLoad since this will only work for the first search. Instead use the following delegate method to do cell registering and customization:

    - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {     [self.searchDisplayController.searchResultsTableView       registerNib:[UINib nibWithNibName:@"YOURCELLNIB" bundle:nil] forCellReuseIdentifier:@"YOURCELLID"];     self.searchDisplayController.searchResultsTableView.separatorColor = [UIColor clearColor]; } 
like image 28
Cedrick Avatar answered Oct 10 '22 15:10

Cedrick