iphoneiosuitableviewstoryboarduisearchresultscontroller

How can i use prototype cell in storyboard with search result controller


I have tableview with search result controller when search in search bar get this error indicate that there is no cell and get the below error .How can create my prototype cell in this method CellForRowAtIndexPath

Code :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
 static NSString *CellIdentifier = @"HoCell";
Ho *cell;
Ho *item;

if (tableView == self.searchDisplayController.searchResultsTableView) {
    if (cell == nil)
    {
        cell = [[Ho alloc]  initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"HoCell"];
    }
    item = [searchResultsController objectAtIndexPath:indexPath];
}
else{
    cell = (Ho*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    item = [fetchedResultsController objectAtIndexPath:indexPath];
}
cell.ho.text = item.name;

cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"se.png"]];

return cell;
}

Error :

*** Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:],  /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:5471
 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

Solution

  • There may be two possibilities Here :

    1) You are returning a number larger than your Array Count from tableView:numberOfRowsInSection:. Don't.

    2) One or more of your cell# outlets is not hooked up in your nib, or is not hooked up to a UITableViewCell (or subclass). Hook them up properly.

    Go through this Ray Wenderlich's Link : How to Add Search Into a Table View

    Check this SO Questions :

    1) UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: Exception

    2) ios 5 UISearchDisplayController crash

    One More Beautiful Link : Custom Prototype Table Cells and Storyboards Just see this Portion :

    - (UITableViewCell *)tableView:(UITableView *)tableView
             cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      UITableViewCell *cell = [tableView
                dequeueReusableCellWithIdentifier:UYLCountryCellIdentifier];
      if (cell == nil)
      {
        [self.countryCellNib instantiateWithOwner:self options:nil];
        cell = self.countryCell;
        self.countryCell = nil;
      }
      // Code omitted to configure the cell...
      return cell;
    }