iphone - UITableView CoreData 断言失败

标签 iphone objective-c ipad core-data tableview

当我的 iPhone 应用程序崩溃时,我的控制台中出现此错误:

*** -[UITableView _endCellAnimationsWithContext:]、/SourceCache/UIKit/UIKit-2372/UITableView.m:1070 中断言失败

它崩溃并在到达这行代码时给我这个:

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

关于我的应用程序以及我是如何做到这一点的一些细节。我有一个带有 TableView 的 View Controller ,它显示我的核心数据对象。我将此 View 设置为显示来自两个实体的数据,因此表第 1 部分显示来自一个实体的对象,然后第 2 部分显示来自第二个实体的对象。

这就是我的问题开始的地方。虽然这效果相当好,但我可以推送我的下一个 View Controller ,并将对象添加到我的第一个实体,然后立即弹回我的 tableviewcontroller,没有问题。但是,如果我尝试将对象添加到第二个实体,则在返回到我的 tableviewcontroller 时,它会崩溃,如上所述。

我无法找出导致此问题的原因,因此我不确定从哪里开始寻找。

来自控制台的一些额外错误信息:

CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)

代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSInteger rows;

    id <NSFetchedResultsSectionInfo> sectionInfoAccounts = [[fetchedResultsControllerAccounts sections] objectAtIndex:0];
    id <NSFetchedResultsSectionInfo> sectionInfoCosts = [[fetchedResultsControllerCosts sections] objectAtIndex:0];

    if (section == 0) rows = [sectionInfoAccounts numberOfObjects];
    else if (section == 1) rows = [sectionInfoCosts numberOfObjects];

    return rows;
}

最佳答案

您正在为 TableView 使用 2 个获取结果 Controller :一个 FRC 用于表部分 0,一个 FRC 用于表部分 1。我在这里有点猜测,但错误可能是在

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

如果为第二个 FRC 调用该函数,则 indexPath/newIndexPath 的节部分为 0(因为 FRC 只有一个节)。但对应的表节是1。

因此,在调用 insertRowsAtIndexPaths:...insertRowsAtIndexPaths:... 等之前,您必须将 FRC 索引路径映射到 TableView 的索引路径。

例如,在插入事件的情况下:

NSIndexPath *newIndexPath1 = [NSIndexPath indexPathForRow:newIndexPath.row inSection:1];
[self.tableView insertItemsAtIndexPaths:@[newIndexPath1]];

否则,第二个实体的新对象将作为第一个 TableView 部分的行插入,这会导致不一致异常。

关于iphone - UITableView CoreData 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12754379/

相关文章:

ios - 为什么我的 tableview 只按升序显示日期而不显示其余数据

iphone - 何时释放整个类中使用的对象?

ios - 提交AppStore审核后描述是否可以更改

iphone - 如何在 iOS 中使用 AvAudiorecorder 将音频录制为 mp3 文件

iphone - NSUserDefaults 和 NSArray 中自定义对象的条件编码

ios - 在表格滚动的自定义表格单元格中添加 subview subview 消失该做什么 subview 返回单元格

objective-c - 在 PDF : Tm or Td/TD? 中查找文本

iphone - 如何在应用程序中启用 iOS 6.0 全景相机?

iphone - 我将如何扭转这个等式?

Objective-C 如何在一行上打印 NSSet(没有尾随逗号/空格)