我試圖從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];
}
歡迎任何建議。我已經嘗試了所有的東西,閱讀了許多類似的問題,但仍然無法弄清楚。謝謝。
錯誤是什麼? – Max
你在使用核心數據嗎? –
您不會從數據庫中刪除任何內容。你所做的就是從數組中移除一個對象。另外,不要在調用'deleteRowsAtIndexPaths ...'後調用'reloadData'。 – rmaddy