美文网首页
iOS - UITableView 和 UISearchBar

iOS - UITableView 和 UISearchBar

作者: 歌白梨 | 来源:发表于2016-08-10 20:02 被阅读575次
*** 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];

相关文章

网友评论

      本文标题:iOS - UITableView 和 UISearchBar

      本文链接:https://www.haomeiwen.com/subject/sayjsttx.html