Tôi có một UITableView hiển thị danh sách các đối tượng được lưu trữ với CoreData. Tôi có thể xóa đối tượng bằng cách sử dụng mã sau:Xóa hàng hoạt ảnh trong UITableView với CoreData cho lỗi xác nhận
- (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];
}
}
Nhưng nó không có hoạt ảnh hoặc thẩm mỹ.
Khi tôi cố gắng động bằng cách thay thế
[self refreshTables];
với
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
tôi nhận được lỗi sau:
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).'
tôi đã cố gắng có deleteRowsAtIndexPaths mã trong một loạt các địa điểm trong mã commitEditingStyle không có lu ck (ví dụ trước khi loại bỏ các đối tượng từ mOC) nhưng tôi không thể có vẻ để có được xung quanh lỗi này.
Tôi biết ví dụ iPhoneCoreDataRecipes của Apple xử lý sự cố bằng cách thiết lập đại biểu cho FetchedResultsController để xử lý chỉnh sửa/xóa hàng, nhưng ở giai đoạn này trong phát triển, nếu có thể, tôi chỉ muốn một giải pháp đơn giản cho hoạt ảnh.
Làm cách nào để tạo hiệu ứng cho việc xóa một hàng, trước/sau khi tôi xóa đối tượng khỏi managedObjectContext của mình?
EDIT: Tôi đã thử xóaRowsAtIndexPaths trước và sau khi xóa mục khỏi mOC, với cùng một lỗi.
# 2 đã khắc phục được sự cố của tôi. Cảm ơn! –