在UISearchResultsTableView中获取“断言失败”

当我开始在SearchBar中键入搜索文本时,我收到以下错误

Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:] 

我的Search Display Controller cellForRowAtIndexPath代码如下:

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

用这个替换你的代码可能会起作用。

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

因为一旦你发现cell对象再次为nil,你就是使用[tableView dequeueReusableCellWithIdentifier:CellIdentifier];分配带有nil对象的单元格[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 这就是为什么它不起作用。