NSFetchedResultsController:不刷新每个新单元格

时间:2011-12-12 14:27:26

标签: objective-c core-data nsfetchedresultscontroller

我注意到在NSFetchedResultsController中使用Core Data插入数据会使用Table View方法刷新tableView:cellForRowAtIndexPath:中的每个单元格。

我的应用程序首先从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导致它为每个单元格调用cellForRowAtIndexPathwillDisplayCell。将其更改为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;
    }
}

1 个答案:

答案 0 :(得分:1)

您是否正在重复使用您的小区(即提供小区重用标识符)?

您是否实施了NSFetchedResultsControllerDelegate方法,这些方法允许您的视图控制器在“联机”而不是[tableView reloadData]时插入/删除行?在这些方法中,您应该使用“[UITableView insertRowsAtIndexPaths:withRowAnimation:][UITableView deleteRowsAtIndexPaths:withRowAnimations:]等来调整引用的行。

最后,您可以将图片加载逻辑移到UITableView委托方法tableView:willDisplayCell:forRowAtIndexPath:中。这样,只有在实际显示时才会调用它。