在两个部分中搜索时崩溃

Ant*_*ond 12 cocoa-touch uitableview uisearchdisplaycontroller nsfetchedresultscontroller ios

我有一个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])'
Run Code Online (Sandbox Code Playgroud)

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
Run Code Online (Sandbox Code Playgroud)

返回1

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

返回2

有趣的是,如果我只有一个部分,那就完美了.

请帮忙!:)

kos*_*kos 38

不知道你是否想出来,我认为你做了但假设你正在使用

[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

在你的cellForRowAtIndexPath中

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

还要确保使用self.tableView

  • 这是因为你要求一个单元格的self.tableView.但是indexPath在搜索时是搜索表的索引路径.索引路径不一定对self.table有效,因此无法将此索引路径传递给它.需要为单元格询问self.table,因为CellIdentifier尚未在搜索表中注册. (9认同)
  • 你能解释一下这个原因吗?只是想知道我可以帮助我解决未来的另一个问题. (3认同)