1

I am trying unsuccessfully to delete a cell from a tableview and also delete its associated record from a database. Here is my code:

- (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];
}

Please any suggestions are welcome. I have tried everything, read many similar questions on SO, still can't figure it out. Thank you.

4

2 回答 2

1

对于从表视图中删除行,您所写的内容是正确的,但在删除行之后不要ReloadData调用deleteRowsAtIndexPaths...。

[self.arr removeObjectAtIndex:indexPath.row];

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

您从未删除数据库中的对象。

于 2013-08-30T16:17:33.000 回答
1

在删除数组调用中的对象之前:

NSManagedObject *eventToDelete = [matchedObjects objectAtIndex:indexPath.row];
[context deleteObject:eventToDelete];
于 2013-08-30T15:49:24.513 回答