2009-06-19 29 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