我有一个UITableView,显示与CoreData一起存储的对象的列表。我可以使用下面的代码删除一个对象:使用CoreData在UITableView中动画行删除给出断言失败
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(@"Delete row");
[managedObjectContext deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
// Save the context.
NSError *error;
if (![managedObjectContext save:&error]) {
/*do this gracefully one day */
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self refreshTables]; //where refreshTables just reloads the data the table is using and calls [self.tableView reloadData];
}
}
但它没有动画或审美。
当我尝试用
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
更换
[self refreshTables];
动画我得到以下错误:
Assertion failure in -[UITableView _endCellAnimationsWithContext:], >/SourceCache/UIKit_Sim/UIKit-1261.5/UITableView.m:920 2010-10-30 16:46:35.717 MyApp[38226:207] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
我已经试过具有deleteRowsAtIndexPaths在代码commitEditingStyle代码中的各种地方都没有lu ck(例如,在从mOC中删除对象之前),但我似乎无法解决此错误。
我知道Apple的iPhoneCoreDataRecipes示例通过为FetchedResultsController设置处理编辑/删除行的代理来处理问题,但在开发的这个阶段,如果可能的话,我只需要一个简单的动画处理这些删除对象的解决方案。
如何在从我的managedObjectContext中移除对象之前/之后为动画删除行?
编辑:我试着从mOC中删除项目之前和之后有deleteRowsAtIndexPaths,具有相同的错误。
#2解决了我的问题。谢谢! – 2011-05-22 23:25:25