2013-08-30 252 views
1

我試圖從tableview中刪除一個單元格並且從數據庫中刪除它的相關記錄失敗。這裏是我的代碼:刪除數據庫中的記錄

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

     // Delete the row from the data source 
     [self.arr removeObjectAtIndex:indexPath.row]; 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [self.tableView reloadData]; 
     NSError *error = nil; 
     [self.context save:&error]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
     [self.tableView reloadData]; 
} 

歡迎任何建議。我已經嘗試了所有的東西,閱讀了許多類似的問題,但仍然無法弄清楚。謝謝。

+1

錯誤是什麼? – Max

+0

你在使用核心數據嗎? –

+2

您不會從數據庫中刪除任何內容。你所做的就是從數組中移除一個對象。另外,不要在調用'deleteRowsAtIndexPaths ...'後調用'reloadData'。 – rmaddy

回答

1

之前刪除的對象的數組電話:

NSManagedObject *eventToDelete = [matchedObjects objectAtIndex:indexPath.row]; 
[context deleteObject:eventToDelete]; 
+0

就是這樣!非常感謝。 – pjv

+0

@pjv正如我在對問題的評論中所說的,您並未從數據庫中刪除數據。 – rmaddy

1

刪除行從表中查看哪些你寫的是正確的,但刪除一行之後不要告訴ReloadDatadeleteRowsAtIndexPaths ....

[self.arr removeObjectAtIndex:indexPath.row]; 

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

您從未刪除數據庫中的對象。