2010-01-06 42 views
4

我有一個管理分組tableView的UITableViewController。 tableView從fetchedResultsController中填充。刪除tableView中的行時出現核心數據錯誤

如果我點擊導航欄的編輯按鈕,然後選擇一列並點擊刪除按鈕,該行被刪除,所有一切都好。

但是,如果我滑動顯示連續的刪除按鈕,然後單擊刪除按鈕,該應用程序崩潰,出現以下錯誤:

2010-01-06 15:25:18.720 Take10[14415:20b] Serious application error. Exception was caught during Core Data change processing: -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1) with userInfo (null)

2010-01-06 15:25:18.721 Take10[14415:20b] Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'

當然,這取決於在誤差變化的索引號我嘗試刪除一行的部分中的行數,該數字比試圖刪除後表部分中的剩餘行數多1。

這裏是我嘗試從fetchedResultsController中刪除數據的代碼。同樣的方法對兩種情況都有反應,所以我不明白它爲什麼在刷卡時崩潰。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

if (editingStyle == UITableViewCellEditingStyleDelete) { 
    // Delete the managed object for the given index path 
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext]; 
    [context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]]; 

    // Save the context. 
    NSError *error = nil; 
    if (![context save:&error]) { 
     /* 
     Replace this implementation with code to handle the error appropriately. 

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 
     */ 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 
    } 
} 

任何想法?

感謝

JK

+0

您是否重新加載controllerDidChangeContent中的表:? – gerry3 2010-01-07 01:01:06

+1

嘗試打破objc_exception_throw並查看拋出異常的方法。這應該有助於追蹤它。 – 2010-01-07 05:39:03

回答

2

一個正常的刷卡和刪除的區別是,後者將調用tableView:willBeginEditingRowAtIndexPathtableView:didEndEditingRowAtIndexPath。實際上,這是抑制插入行的縮進和顯示的好方法。

另一個區別是在刪除後立即調用setEditing:(使用NO作爲參數值)。

在您定義/覆蓋的三個函數中的任何一箇中設置斷點,並查看是否可以縮小它發生的位置。

相關問題