当我的 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/