2009-06-19 43 views
21

我使用Matt Gallagher的GenericTableViewController想法来控制我的UITableViews。我的数据源是NSFetchedResultsController我该如何制作deleteRowsAtIndexPaths:使用GenericTableViewController?

http://cocoawithlove.com/2008/12/heterogeneous-cells-in.html

一切工作正常,直到我尝试删除单元格。

我在我的视图控制器下面的代码:

- (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]; 
    } 
} 

最后一行在控制台中的相当冗长的解释崩溃:

*** 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,我明白它在说什么。 ..行不会被删除(我会假设),因为我没有转发一些消息到正确的地方(因为我已经从它的'正常'位置移动了一些代码)...任何人都知道哪一个?我完全沉迷于这一个。

回答

67

嗯,嗯。我刚刚发现this答案,这是不一样的,但让我朝着正确的方向前进。我将在此留给任何未来有类似麻烦的人。

,关键是包装与开始和结束标记的deleteRowsAtIndexPaths,并强制模式相同的块内更新,从而导致:

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

这导致问题走开,和动画工作完美。

+4

+1非常感谢您的回答。 – 2010-03-31 10:17:55