【问题标题】:Crash when searching in two section在两个部分中搜索时崩溃
【发布时间】:2012-12-29 11:03:43
【问题描述】:

我有一个NSFetchedResultController 不同的部分。 当我尝试使用 UISearchDisplayController 进行搜索时发生崩溃:

*** Assertion failure in -[UITableViewRowData rectForRow:inSection:], /SourceCache/UIKit/UIKit-2372/UITableViewRowData.m:1630

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath 0x1d2c4120> 2 indexes [0, 1])'

我检查了,我的搜索数组确实有两个条目(预期结果):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

返回 1

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

返回 2

有趣的是,如果我只有一个部分,它就可以完美运行。

请帮忙! :)

【问题讨论】:

    标签: ios uitableview cocoa-touch nsfetchedresultscontroller uisearchdisplaycontroller


    【解决方案1】:

    不确定你是否发现了,我想你做到了,但假设你正在使用

    [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    

    在你的 cellForRowAtIndexPath 中

     if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     } else {
        cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
     }
    

    还要确保您使用的是 self.tableView

    【讨论】:

    • 您能解释一下原因吗?只是想知道它可能会帮助我在未来解决另一个问题。
    • 这是因为您正在向 self.tableView 询问一个单元格。但是 indexPath 在搜索时是搜索表的索引路径。索引路径不一定对self.table有效,所以这个索引路径不能传递给它。需要向 self.table 询问单元格,因为 CellIdentifier 尚未在搜索表中注册。
    • tableView 和 self.tableView 真的很不一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多