Getting “Assertion failure” in UISearchResultsTableView

假装没事ソ 提交于 2019-12-23 04:37:06

问题


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.



来源:https://stackoverflow.com/questions/26686684/getting-assertion-failure-in-uisearchresultstableview

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