iPad SearchViewController崩溃_PFArray objectAtIndex超出界限

时间:2015-06-05 10:55:39

标签: ios objective-c ipad core-data uisearchresultscontroller

我正在处理我的应用程序的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
  5. 对tableView进行排序
  6. 我在搜索栏上输入,应用程序立即崩溃
  7. 如果我使用带有2或3个字母的searchString执行相同的方案,那么searchViewController返回的项目数量会更大,那么就不会崩溃。

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

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

    感谢阅读。

1 个答案:

答案 0 :(得分: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];

当用户停止使用此方法进行搜索并且不再崩溃时,我尝试清空filteredList:

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    self.filteredList=nil;
}