nemesis
nemesis

Reputation: 1349

Properly deleting an item from UITableView

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

Upvotes: 1

Views: 1514

Answers (2)

Krrish
Krrish

Reputation: 2256

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

Upvotes: 3

Ilanchezhian
Ilanchezhian

Reputation: 17478

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.
    }   
}

Upvotes: 3

Related Questions