我有一個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