1

Could anybody please explain how to properly implement the tableView:commitEditingStyle:forRowAtIndexPath: method so that I could properly delete an item from the backing array and display only the items that I haven't deleted yet?

Here's what I have so far:

@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
    IBOutlet UITableView *tableView;

    NSArray *allItems;
    NSMutableArray *displayItems;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    allItems = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven", nil];
    displayItems = [[NSMutableArray alloc] initWithArray:allItems];
}

-(UITableViewCell*) tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = [displayItems objectAtIndex:indexPath.row];
    return cell;
}

@end
Ash Furrow
  • 12,391
  • 3
  • 57
  • 92
nemesis
  • 1,349
  • 1
  • 15
  • 30

2 Answers2

3

Try this:

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        if(!deletedIndices) deletedIndices = [[NSMutableIndexSet alloc] initWithIndex:indexPath.row];
        else [deletedIndices addIndex:indexPath.row];
    }   
    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.
    }   
}

-(IBAction)editingDidComplete:(id)sender
{

    // remove the objects from display array
    [valueArray removeObjectsAtIndexes:deletedIndices];
    // Reload tableView if needed
    [mainTableView reloadData];
    [mainTableView setEditing:NO];
 }
Krrish
  • 2,256
  • 18
  • 21
  • It crashed : Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (7) must be equal to the number of rows contained in that section before the update (7), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' – nemesis Mar 19 '12 at 16:40
3

Implement your tableView:commitEditingStyle:forRowAtIndexPath: as follows:

// Override to support editing the table view.
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        // remove the object from display array
        [displayItems removeObjectAtIndex:indexPath.row];

        // Delete the row from the data source.
        [aTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }   
    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.
    }   
}
Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
  • It crashed : Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (7) must be equal to the number of rows contained in that section before the update (7), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' – nemesis Mar 19 '12 at 16:40
  • Well, actually it started working a little bit, it deleted items even if it deleted items that I didn't intend to delete. These days I will spend some more time on further development, I will get it to work ;-) – nemesis Mar 21 '12 at 13:48