2009-06-19 28 views
21

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.

Trả lời

67

Vâng, bah. Tôi chỉ tìm thấy this câu trả lời, mà không phải là như nhau, nhưng đã cho tôi đi đúng hướng. Tôi sẽ để cái này ở đây cho bất cứ ai trong tương lai có những rắc rối tương tự.

Điều quan trọng là để quấn deleteRowsAtIndexPaths với bắt đầu và thẻ kết thúc, và buộc các mô hình để cập nhật trong cùng một khối, kết quả là:

[tableView beginUpdates]; 
[self constructTableGroups]; 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
     withRowAnimation:UITableViewRowAnimationFade]; 
[tableView endUpdates]; 

này gây ra vấn đề này ra đi, và hình ảnh động để chỉ hoạt động hoàn hảo.

+4

+1 Cảm ơn rất nhiều câu trả lời. –

+2

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

+0

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

Các vấn đề liên quan