我注意到插入数据在Core Data
将使用tableView:cellForRowAtIndexPath:
方法刷新我的Table View
中的每个单元格。NSFetchedResultsController:不刷新每个新的单元格
我的应用程序首先从Web服务中获取100多个项目。将它们添加到核心数据数据库后,将为每个单元拨打一个tableView:cellForRowAtIndexPath:
呼叫。我期望只刷新第一个8-9可见单元格。我该如何改变它?这是一个问题的原因是我的应用程序在第一次调用tableView:cellForRowAtIndexPath
时下载封面图像 - 并且在每个单元调用它时,这不是最佳解决方案。
EDIT1: 我的代码是完全基于来自CS 192p course at Stanford University一个例子。我已经从讲座14下载了它们的Photomania.zip
,并插入了tableView:willDisplayCell:forRowAtIndexPath:
方法,该方法在调用方法时在日志中显示项目名称。你可以在这里获取我的代码:http://uploads.demaweb.dk/Photomania.zip。 正如你所看到的,它看起来像这个方法被称为表中的每个项目?为什么?
EDIT2: 我在iOS Developer Library的NSFetchedResultsControllerDelegate
中使用了这种方法。我发现withRowAnimation:UITableViewRowAnimationFade
导致它为每个单元调用cellForRowAtIndexPath
和willDisplayCell
。将其更改为withRowAnimation:UITableViewRowAnimationNone
时,仅调用可见单元格。
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
是的,我正在使用'NSFetchedResultsControllerDelegate'协议,它的实现几乎像文档:http://developer.apple.com/library/ios/#documentation/CoreData/Reference/NSFetchedResultsControllerDelegate_Protocol/Reference/Reference。 HTML。 这是正常的,当所有的数据变得可用时,NSFetchedResultsControllerDelegate会产生一个'tableView:cellForRowAtIndexPath'? 感谢您关于'tableView:willDisplayCell:forRowAtIndexPath:'的通知。这可能会解决我的问题,但我仍然不同寻常的是,每个细胞都会调用细胞方法。 – dhrm
'tableView:willDisplayCell:forRowAtIndexPath:'不起作用。请看我的EDIT1。 – dhrm
使用'NSFetchedResultsController'时,只有两种方式调用'tableView:cellForRowAtIndexPath:',当您在tableView上显式调用'reloadData'时,或者当您插入/删除行/部分以响应委托调用时。您需要通过调试器监视代码中的那些区域,以查看导致完整表重新加载的原因。 “NSFetchedResultsCoordinator”绝不直接与表格视图本身交互。 – gschandler