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

本秂侑毒 提交于 2019-12-21 17:55:48

问题


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:'

回答1:


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;
}



回答2:


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.




回答3:


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...



来源:https://stackoverflow.com/questions/14191841/how-can-i-use-prototype-cell-in-storyboard-with-search-result-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!