*** Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.12/UITableView.m:7971
报错了。
情景:UITableView 和UISearchBar 组合使用,设置了代理,UISearchResultsUpdating
原因:当点击searchBar时,会调用- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法,此方法将会出现问题,方法示例:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"------%@,%@",[tableView class],[self.tableView class]);
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
cell.textLabel.text = self.dataSource[indexPath.row];
return cell;
}
此处的tableView是UISearchResultsTableView,通过打印出来的结果可以看到, 而当UISearchResultsTableView去它的内存池中寻找标示为reuseIdentifier的Cell的时候就会出现错误,而出现UISearchResultsTableView而不是原来的tableview的原因是我在xib中使用了UISearchBar。
解决方案有两个:
1.将tableview
替换成self.tableview
就可以了。
2.把UISearchBar改成UISearchViewController,初始化时这样设为nil[[UISearchController alloc] initWithSearchResultsController:nil];
。