4.8. Enabling Swipe Deletion of Table View Cells

Problem

You want your application users to be able to delete rows from a table view easily.

Solution

Implement the tableView:editingStyleForRowAtIndexPath: selector in the delegate and the tableView:commitEditingStyle:forRowAtIndexPath: selector in the data source of your table view:

Table view cells with indentation

Figure 4-5. Table view cells with indentation

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

  UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;

  if ([tableView isEqual:self.myTableView]){
    result = UITableViewCellEditingStyleDelete;
  }

  return result;

}

- (void) setEditing:(BOOL)editing
           animated:(BOOL)animated{

  [super setEditing:editing
           animated:animated];

  [self.myTableView setEditing:editing
                      animated:animated];


}

- (void)  tableView:(UITableView *)tableView
 commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
  forRowAtIndexPath:(NSIndexPath *)indexPath{

  if (editingStyle == UITableViewCellEditingStyleDelete){

    if (indexPath.row < [self.arrayOfRows count]){

      /* First remove this object from the source */
      [self.arrayOfRows removeObjectAtIndex:indexPath.row];

      /* Then remove the associated cell from the Table View */
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                       withRowAnimation:UITableViewRowAnimationLeft];

    }
  }

}

The tableView:editingStyleForRowAtIndexPath: method can enable deletions. It is called by the table view and its return value determines what the table view allows the user to do (insertion, deletion, etc.). The tableView:commitEditingStyle:forRowAtIndexPath: method carries out the user’s requested deletion. The latter method is defined in the delegate, but its functionality is a bit overloaded: not only do you use the method to delete data, but you also have to delete rows from the table here.

Discussion

The table view responds to the swipe by showing a button on the right side of the targeted row (Figure 4-6). As you can see, the table view is not in editing mode, but the button allows the user to delete the row.

This mode is enabled by implementing the tableView:editingStyleForRowAtIndexPath: method (declared in the UITableViewDelegate protocol), whose return value indicates whether the table should allow insertions, deletions, both, or neither. By implementing the tableView:commitEditingStyle:forRowAtIndexPath: method in the data source of a table view, you can then get notified if a user has performed an insertion or deletion.

Delete button appearing on a table view cell

Figure 4-6. Delete button appearing on a table view cell

The second parameter of the deleteRowsAtIndexPaths:withRowAnimation: method allows you to specify an animation method that will be performed when rows are deleted from a table view. Our example specifies that we want rows to disappear by moving from right to left when deleted.

Get iOS 6 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.