Objective c UISearchDisplayController和CoreData“索引0处的节中索引2处没有对象”
我正在使用CoreData存储一些数据,但数量不大。我决定在我的TVC中添加一个显示数据的UISearchDisplayController,这样用户就可以通过文本进行搜索。要在CD中搜索,我使用第二个NSFetchedResultsController。问题是我得到了以下错误:“索引0处的节中索引2处没有对象” 我已经检查了几个方面来解决问题: 从数据库中提取了多少对象, numberOfSectionsInTableView:,tableView:numberOfRowsInSection给出的值是多少 我还检查了传递给这两个方法的tableView是否指向self.searchDisplayController.searchResultsTableView我认为是正确的 FetchedObject中的对象数与tableView:numberOfRowsInSection返回的值相同: 在fetch请求中,我只有一个sectionNameKeyPath=nil节 我发现了几个类似的问题,但没有任何帮助。我的实现基于 我做错了什么?哪里可能有错误?如果你需要更多信息,请告诉我。提前谢谢 编辑: 我最近晚上一直在做这件事。早上,当我带着新鲜的头脑回到代码中时,我意识到了一些事情 解决方案 我一直在使用这样的代码:Objective c UISearchDisplayController和CoreData“索引0处的节中索引2处没有对象”,objective-c,core-data,ios7,uisearchdisplaycontroller,Objective C,Core Data,Ios7,Uisearchdisplaycontroller,我正在使用CoreData存储一些数据,但数量不大。我决定在我的TVC中添加一个显示数据的UISearchDisplayController,这样用户就可以通过文本进行搜索。要在CD中搜索,我使用第二个NSFetchedResultsController。问题是我得到了以下错误:“索引0处的节中索引2处没有对象” 我已经检查了几个方面来解决问题: 从数据库中提取了多少对象, numberOfSectionsInTableView:,tableView:numberOfRowsInSection给
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
.
.
.
UITableView *tableView = controller == self.fetchedResultsController ? self.tableView : self.searchDisplayController.searchResultsTableView;
.
.
.
}
当我在搜索开始时self.fetchedResultsController=nil时,是什么导致了错误
而且我一直使用相同的fetchedResultsController。我上面链接的代码中的[self-fetchedResultsControllerForTableView:tableView]方法修复了这个问题。确保您正在使用适当的fetchedResultsController!即使你是,再次检查
在此之后,我应该得到我的搜索结果。。。但我没有
另一件事是使用[tableView dequeueReusableCellWithIdentifier:CellIdentifier];。看起来还可以,但在searchDisplayController的tableView中,我没有给定标识符的单元格,所以我需要将它们从self.tableView中排出
现在我有了我的搜索结果
如果您没有自己的,这也有助于:
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
[tableView registerClass:[NewsCell class] forCellReuseIdentifier:identifier];
}
感谢阅读我的问题并花一分钟思考解决方案的所有人。您检查了所有方法中的表视图吗?@Wain感谢您提出这个问题。再次检查时,我发现问题就在它旁边。