KillaIce
KillaIce

Reputation: 105

Swipe to delete UITableView

How can I delete a UITableView row on swipe? I know the question has been asked, but I get only errors. Here's the code:

-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
    forRowAtIndexPath:(NSIndexPath *)indexPath {
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete) {
         NSInteger row = [indexPath row];
         [tableDataSource removeObjectAtIndex:row];      
    }
}

But nothing happens. I would appreciate if you write code.

Upvotes: 8

Views: 8884

Answers (2)

Benoît
Benoît

Reputation: 7427

Better is just to delete the selected row, not reload all the table:

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

Upvotes: 45

ahmet emrah
ahmet emrah

Reputation: 1858

reload table after removing the row.

[tableView reloadData];

Upvotes: 7

Related Questions