我使用核心数据fetchResultController作为表数据源。一切正常,但在从coredata删除批量商品时遇到了特殊问题。得到波纹管错误并且表格变成空白
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 (339) must be equal to the number of rows contained in that section before the update (338), plus or minus the number of rows inserted or deleted from that section (0 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)
我正在使用以下代码
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.dataTableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.dataTableView endUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.dataTableView;
switch(type) {
case NSFetchedResultsChangeDelete:{
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
更新: 这是我的numberOfRows方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ([self fetchedResultsController]) {
id sectionInfo = [[[self fetchedResultsController] sections] objectAtIndex:0];
NSInteger rowCount = [sectionInfo numberOfObjects];
return rowCount;
}
return 0;
}