How to enable swipe to delete cell in a TableView?

Amogh Talpallikar picture Amogh Talpallikar · Jan 24, 2012 · Viewed 84.5k times · Source

I have a UIViewController that implements TableViews delegate and datasource protocols. Now I want to add "swipe to delete" gesture to cells.

How should I go about it.

I have given a blank implementation of commitEditingStyle method and also set the Editing property to YES.

Still the swipe feature is not coming .

Now Do I need to separately add UISwipeGesture to each cell ?

Or am I missing something ?

Answer

Dj S picture Dj S · Oct 7, 2013

As Dan has commented above, you need to implement the following table view delegate methods:

  1. tableView:canEditRowAtIndexPath:
  2. tableView:commitEditingStyle:forRowAtIndexPath:

Note: I have tried this in iOS 6 and iOS 7.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return YES - we will be able to delete all rows
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Perform the real delete action here. Note: you may need to check editing style
    //   if you do not perform delete only.
    NSLog(@"Deleted row.");
}