I have a BarButton Item and the name of it is Edit when it is in edit mode, and Done when it is done with editing. So when the user clicks on edit the editpress method gets executed (see code).
With that the, commitEditingStyle , and the cell and the record (from the array which is used to add data to the cell) is deleted.
The problem : When i click on delete, the cell gets deleted from the table correctly. But not from the array which is used to load the cells. When i placed a debug point at commitEditingStyle, it only executes that code when i FIRST click on delete. If i delete records for the 2nd, 3rd etc this code block is not executed. Why is this ?
I have not added [self.tableView setEditing:YES animated:YES]; record in commitEditingStyle. Is that the flaw.
note: i don’t think my delete from array logic is incorrect, because the same logic works in other programs (gurantted, and that’s why i didnt post it)
-(void) editpress:(id)sender{
UIBarButtonItem *editButton = (UIBarButtonItem*)self.navigationItem.leftBarButtonItem;
if (!self.tableView.editing) {
[self.tableView setEditing:YES animated:YES];
UIButton *rbut= [UIButton buttonWithType:UIButtonTypeCustom];
[rbut setImage:rightImage forState:UIControlStateNormal];
[rbut addTarget:self action:@selector(editpress:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rBarButDone = [[UIBarButtonItem alloc] initWithCustomView:rbut];
self.navigationItem.rightBarButtonItem = rBarButDone;
}
else {
[self.tableView setEditing:NO animated:YES];
UIButton *rbut= [UIButton buttonWithType:UIButtonTypeCustom];
[rbut setImage:rightImage forState:UIControlStateNormal];
[rbut addTarget:self action:@selector(editpress:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rBarButEdit = [[UIBarButtonItem alloc] initWithCustomView:rbut];
self.navigationItem.rightBarButtonItem = rBarButEdit;
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.tableView beginUpdates];
// I write the logic to delete the record from the array and the table here.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];
[self.tableView endUpdates];
}
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
}
}
The code is obviously executing the commitEditingStyle. However, after you delete, does the cellForRowAtIndexPath now return the correct data for the row that was deleted? If you’re reading that data from an internal array that you downloaded, then you need to delete an item from that internal array. If you’re reading from another source, you need to either make sure that source is sending the correct data, or you’re refreshing it after deleting the row. In either case, whatever your “web service” is doing to delete the data is not refreshing the cell with new data. Also, you’ll need to report one fewer in numberOfRowsInSection: method.