Getting “Assertion failure” in UISearchResultsTabl

2019-06-09 11:47发布

问题:

I'm getting the following error when i start type the search text in SearchBar

Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:]

My cellForRowAtIndexPath code for Search Display Controller is as follows :

if(tableView == self.searchDisplayController.searchResultsTableView)
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [search objectAtIndex:indexPath.row];

    return cell;
}

回答1:

Replace your code with this one may be it will work.

if(tableView == self.searchDisplayController.searchResultsTableView)
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [search objectAtIndex:indexPath.row];

    return cell;
}

because once you find that the cell object is nil again you are allocating the cell with nil object using [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; that's why it is not working.