0

我正在开发我的应用程序的 iPad 版本,但遇到了一个奇怪的问题。

这是应用程序的结构:

UISplitViewController
->MasterViewController
-->NavigationController
--->TableViewController
---->PrototypeCells
--->SearchDisplayController
->DetailViewController
-->NavigationController
--->TableViewController
---->StaticCells

基本上,应用程序在 MasterView 上显示了 40 000 行,其中包含 CoreData 和 fetchResultController。用户可以使用 searchDisplayController 搜索项目或使用新的 FetchResultController 对 TableView 进行排序。当点击一行时,didSelectRowAtIndexPath 方法在 detailView 中设置 Item。

问题是,有时当我单击 searchBar 时,应用程序崩溃并显示以下错误消息:

**Terminating app due to uncaught exception 'NSRangeException', 
reason: ' -[_PFArray objectAtIndex:]: index (11) beyond bounds (X)'**

但我想我发现了一个让应用程序每次崩溃的场景:

  1. 应用程序负载
  2. 我搜索包含 5 个或 6 个字母的 searchString,因此 searchViewController 返回的项目数量非常少(如 5 个项目)。我认为“越界(X)”对应于这个数量的项目。
  3. 我取消搜索
  4. 我使用新的 FetchResultController 对 tableView 进行排序
  5. 我在搜索栏上输入,应用程序立即崩溃

如果我执行相同的场景,但使用带有 2 个或 3 个字母的 searchString,因此 searchViewController 返回的项目数量更大,则不会发生崩溃。

此外,当我应用此方案时,iPhone 版本没有问题/崩溃。

我不明白问题出在哪里,这让我发疯。有人知道出了什么问题吗?我可以更新这篇文章以添加代码,但现在,我不知道哪个部分对理解崩溃有用。

谢谢阅读。

4

1 回答 1

0

所以我想我找到了解决办法。

在我的 TableViewController 中玩断点后,我注意到点击 searchBar 时崩溃的方法是cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ItemCell *cell = [self.tableView dequeueReusableCellWithIdentifier:ItemCellIdentifier forIndexPath:indexPath];
    Item *item = nil;
    if (self.searchDisplayController.active) {
        item = [self.filteredList objectAtIndex:indexPath.row];
    } else {
        item = [self.fetchedResultsController objectAtIndexPath:indexPath];
    }
    //...
    return cell;
}

特别是这一行:item = [self.filteredList objectAtIndex:indexPath.row];

当用户停止使用此方法搜索并且不再崩溃时,我尝试清空过滤列表:

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    self.filteredList=nil;
}
于 2015-06-05T13:43:47.973 回答