ios - 如何使用 NSFetchedResultsController 更新 UITableView

标签 ios

TableViewCell 未创建,而 numberOfRowsInSection 中的对象数量多于形成的单元格总数

最佳答案

您可以为获取的结果 Controller 设置委托(delegate),该 Controller 注册以从其托管对象上下文接收更改通知。影响结果集或部分信息的上下文中的任何更改都会被处理,并相应地更新结果。当结果对象更改位置或修改部分时, Controller 会通知委托(delegate)(请参阅 NSFetchedResultsControllerDelegate)。您通常使用这些方法来更新 TableView 的显示。

您可以使用controllerWillChangeContent: 和controllerDidChangeContent: 将更新括起来到 TableView ,其内容由获取的结果 Controller 提供,如图所示。

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}


- (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;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
    atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
    newIndexPath:(NSIndexPath

 *)newIndexPath {


UITableView *tableView = self.tableView;

switch(type) {

    case NSFetchedResultsChangeInsert:
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                   withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                   withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeUpdate:
        [self configureCell:[tableView cellForRowAtIndexPath:indexPath]
              atIndexPath:indexPath];
        break;

    case NSFetchedResultsChangeMove:
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                   withRowAnimation:UITableViewRowAnimationFade];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                   withRowAnimation:UITableViewRowAnimationFade];
        break;
}
}




- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}

关于ios - 如何使用 NSFetchedResultsController 更新 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57991021/

相关文章:

ios - 检测点击 UIImageView IOS

ios - 在 Swift 上的哪里存储 API key ?

ios - Swift 3 - 在启动时加载多个 ViewController

ios - 调整 UILabel 的大小以适应自定义 UITableViewCell 中的文本,无论宽度如何

ios - Xcode 将屏幕分成 4 个大按钮

ios - Swift 中奇怪类型的数组

ios - 当我更改 statusBarOrientation 时应用程序生命周期错误

iphone - NSZombieEnabled 似乎修复了我的错误

ios - 你的第二个 iOS 应用教程 : How Many Sections Do They Want?

iphone - 使用仪器进行内存分析