4

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:'
Dalee Davis
  • 981
  • 8
  • 19
wod
  • 812
  • 1
  • 10
  • 23

3 Answers3

4

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;
}
Community
  • 1
  • 1
Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • I use storyboard with prototype cell and make custom class for prototype cell called (Ho) , and check there no problem with number of rows in section.I investigate that the problem in cellForRowAtIndexPath – wod Jan 07 '13 at 08:24
  • 1
    Just go through this Link : http://useyourloaf.com/blog/2012/09/06/search-bar-table-view-storyboard.html – Bhavin Jan 07 '13 at 08:27
  • Thanks Bhavin but still used the standard prototype cell what about if i used custom class for prototype cell. – wod Jan 07 '13 at 08:51
  • Ok.. I have added one more link in My Answer. Just Check it and let me know if your problem is solved or not. – Bhavin Jan 07 '13 at 09:02
0

Your code seems to be buggy. You check for cell == nil while it is not set to nil initially. It also looks a bit strange that you allocate your cells in that way based on search mode.

Anyways, I would do it different way. Imho the way I do it is almost canonical :) Just use your search result to populate your cells with correct data for each case (search mode and normal mode). In this example, searchResult and dataSource are arrays with strings. I think in real life for you it will be something more complex like array of nsdictionary.

In your view controller:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)_section
{
        /* Use correct data array so that in search mode we draw cells correctly. */
        NSMutableArray *data = searching ? searchResult : dataSource;
        return [data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{       
        /* Use correct data array so that in search mode we draw cells correctly. */
        NSMutableArray *data = searching ? searchResult : dataSource;
        static NSString *CellIdentifier = @"CellId";

        CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
                cell = [[[CustomTableViewCell alloc] initWithIdentifier:CellIdentifier] autorelease];
        }

        /* Note this is only correct for the case that you have one section */
        NSString *text = [data objectAtIndex:[indexPath row]]

        cell.textLabel.text = text;
        /* More setup for the cell. */
        return text;
}

And here are delegate methods for search controller and some helpers:

- (void) searchTableView
{
        NSString *searchText = searchBar.text;

        for (NSString *item in dataSource) {
                NSRange range = [item rangeOfString:searchText options:NSCaseInsensitiveSearch];
                if (range.length > 0) {
                        [searchResult addObject:item];
                }
        }
}

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
        searching = NO;
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
        searching = NO;
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0]
                      withRowAnimation:UITableViewRowAnimationAutomatic];
        [searchResult removeAllObjects];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchText
{
        [searchResult removeAllObjects];

        if ([searchText length] > 0) {
                searching = YES;
                [self searchTableView];
        } else {
                searching = NO;
        }
        return YES;
}

Hope this helps.

Cynichniy Bandera
  • 5,991
  • 2
  • 29
  • 33
0

I have the same issue, here is what is did and it is now working perfectly..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Here is my code BEFORE
    //
    OPUsersTableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:TableCellID];

    // Here is my code AFTER
    //
    OPUsersTableViewCell *tableCell = [self.groupTableView dequeueReusableCellWithIdentifier:TableCellID];

Note:

self.groupTableView is where the prototype cell is...

0yeoj
  • 4,500
  • 3
  • 23
  • 41