2013-09-05 60 views
1

我有一個UITableView,它使用NSFetchedResultsController作爲其數據源。我爲我的部分使用了其中一個字段。我啓用了行刪除(使用滑動手勢)。工作得很好。錯誤消息使用NSFetchedResultsController刪除部分的最後一行

當我刪除某一節的最後一行時,出現問題。它不會崩潰在我身上,但在控制檯顯示以下信息:

CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)

這都是非常基本的東西:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    [[self tableView] beginUpdates]; 
} 

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
     atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
     newIndexPath:(NSIndexPath *)newIndexPath { 
    UITableView *tableView = self.tableView; 

    switch(type) { 
     … 
     case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
          withRowAnimation:UITableViewRowAnimationFade]; 

      break; 

     case NSFetchedResultsChangeUpdate: 
      [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
      [self configureCell:[tableView cellForRowAtIndexPath:indexPath] 
        atIndexPath:indexPath]; 
      break; 
     … 
    } 
} 

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    [[self tableView] endUpdates]; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source    
     FHClass *foo = [[self fetchedResultsController] objectAtIndexPath:indexPath];; 
     [[self managedObjectContext] deleteObject:meal]; 
     [self saveContext]; 
    } 
} 

如果我檢查-numberOfRowsInSection:我看到這個數字正在穩步下降時刪除行。甚至到了0的地步。

我錯過了什麼?任何提示?我的愚蠢的錯誤? ;-)

回答

2

您應該執行controller:didChangeSection:atIndex:forChangeType:來處理部分更改,然後請求表視圖到deleteSections:

+0

好吧,那很容易。非常感謝! – flohei

相關問題