【问题标题】:Error message deleting the last row of a section using NSFetchedResultsController使用 NSFetchedResultsController 删除部分最后一行的错误消息
【发布时间】:2013-09-05 15:18:10
【问题描述】:

我有一个UITableView,它使用NSFetchedResultsController 作为它的数据源。我将其中一个字段用于我的部分。我启用了行删除(使用滑动手势)。工作得很好。

当我删除部分的最后一行时出现问题。它不会在我身上崩溃,但控制台会显示以下消息:

CoreData:错误:严重的应用程序错误。捕获到异常 在调用期间从 NSFetchedResultsController 的委托 -controllerDidChangeContent:。无效更新:第 0 节中的行数无效。现有节中包含的行数 更新后(1)必须等于包含的行数 更新前的那个部分(1),加上或减去行数 从该部分插入或删除(0 插入,1 删除)和加上 或减去移入或移出该部分的行数(0 移动 入,0 移出)。与 userInfo (null)

这些都是非常基本的东西:

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

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {
    UITableView *tableView = self.tableView;

    switch(type) {
        …
        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];

            break;

        case NSFetchedResultsChangeUpdate:
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath]
                    atIndexPath:indexPath];
            break;
        …
    }
}

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

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source            
        FHClass *foo = [[self fetchedResultsController] objectAtIndexPath:indexPath];;
        [[self managedObjectContext] deleteObject:meal];
        [self saveContext];
    }  
}

如果我检查-numberOfRowsInSection:,我发现删除行时该数字正在稳步减少。甚至到了上面写着0的地步。

我错过了什么?有什么提示吗?我犯了愚蠢的错误? ;-)

【问题讨论】:

    标签: ios uitableview core-data nsfetchedresultscontroller


    【解决方案1】:

    您应该实现controller:didChangeSection:atIndex:forChangeType: 来处理部分更改,然后向deleteSections: 请求表格视图。

    【讨论】:

    • 嗯,这很简单。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 2013-11-10
    相关资源
    最近更新 更多