Tôi đang sử dụng ý tưởng GenericTableViewController
của Matt Gallagher để kiểm soát UITableViews
của tôi. Nguồn dữ liệu của tôi là NSFetchedResultsController
.Làm thế nào tôi có thể làm cho deleteRowsAtIndexPaths: làm việc với GenericTableViewController?
http://cocoawithlove.com/2008/12/heterogeneous-cells-in.html
Mọi thứ đều hoạt động tốt, cho đến khi tôi cố xóa ô.
Tôi có đoạn mã sau vào View Controller của tôi:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object.
NSManagedObjectContext *context = [wineryController managedObjectContext];
[context deleteObject:[wineryController objectAtIndexPath:indexPath]];
NSError *error;
if (![context save:&error]) {
// Handle the error.
}
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Dòng cuối cùng bị treo với lời giải thích khá dài dòng trong bảng điều khiển
*** 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 (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
OK, tôi hiểu những gì nó nói. .. một hàng là không nhận được xóa (tôi sẽ giả định) bởi vì tôi không chuyển tiếp một số tin nhắn đến đúng nơi (kể từ khi tôi đã di chuyển một số mã từ vị trí 'bình thường' của nó) ... bất cứ ai có bất kỳ ý tưởng đó? Tôi hoàn toàn bối rối về điều này.
+1 Cảm ơn rất nhiều câu trả lời. –
Bạn được phép sửa đổi nguồn dữ liệu trước khối hoạt ảnh. Lỗi mà bạn đã thấy cho biết rằng bạn chưa thay đổi nguồn dữ liệu trước khi làm hoạt ảnh xóa. Đảm bảo rằng nguồn dữ liệu ở trạng thái cuối cùng (sau khi xóa) trước khi bạn tạo hoạt ảnh cho việc xóa các hàng đó. – Mark
Tôi luôn quên điều này ... tại sao mã mẫu XCode không bao gồm những dòng này? – Echelon