带有NSFetchedResultsController的表移动添加部分的行

我有UITableView与NSFetchedResultsController

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:[self dataFetchRequest] managedObjectContext:[MLCoreDataService sharedInstance].managedObjectContext sectionNameKeyPath:@"checked" cacheName:nil]; aFetchedResultsController.delegate = self; 

我已经实施

 - (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:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeDelete: [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeUpdate: [(MLItemTableViewCell*)[tableView cellForRowAtIndexPath:indexPath] setItem:anObject]; break; case NSFetchedResultsChangeMove: [tableView moveRowAtIndexPath:indexPath toIndexPath:newIndexPath]; break; } [self.tableView reloadData]; } 

当我在我的行中更改实体的“检查”属性时,我接到一个电话

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

与typesNSFetchedResultsChangeMove

在编辑之前和之后我有相同的部分数量时,所有的都是正确的。 但是当我只有一个部分,编辑行后应该去下一节我得到了以下错误

 2014-07-27 17:37:43.384 mlist[34340:60b] CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. Invalid update: invalid number of sections. The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). with userInfo (null) 2014-07-27 17:37:43.420 mlist[34340:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).' 

当我尝试在types为NSFetchedResultsChangeMove的didChangeObject …方法上插入删除时,我有与部分中的行数相同的错误。

有没有常见的方法来解决它? 什么是正确的方法呢?

通过自定义调用解决的问题

 [self.tableView beginUpdates]; [self.tableView endUpdates]; 

NSFetchedResultsControllerDelegate的整个代码

 - (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:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; } break; case NSFetchedResultsChangeDelete:{ [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } break; case NSFetchedResultsChangeUpdate:{ [(MLItemTableViewCell*)[tableView cellForRowAtIndexPath:indexPath] setItem:anObject]; } break; case NSFetchedResultsChangeMove:{ if (_sectionsChanged) { [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; _sectionsChanged = NO; } else { [tableView moveRowAtIndexPath:indexPath toIndexPath:newIndexPath]; } } break; } } - (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; } _sectionsChanged = YES; } - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [self.tableView beginUpdates]; } - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.tableView endUpdates]; }