Question

Is there any delegate method called the moment one swipe on table cell and delete button comes in from the right?

I wanted to track the swipe and do some action there.

Also, which delegate method gets called when you tap on "Delete" button in the cell.

alt text

Was it helpful?

Solution

The UITableViewDelegate method tableView:editingStyleForRowAtIndexPath: will be called before a row enters "edit" mode. This is called when you swipe a cell as well as when the table view receives the setEditing:animated message. If you have an Edit button that puts the table view into edit mode you need to be aware that will be called for each visible cell.

So you can do something like:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.editing) {
        return UITableViewCellEditingStyleDelete;
    }
    else {
        // do your thing
        return UITableViewCellEditingStyleDelete;
    }
}

When you tap the Delete button tableView:commitEditingStyle:forRowAtIndexPath: gets called.

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // repsond to delete
    }     
}

And if you want to change the text of the Delete button you can use tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:

If on the other side you don't want to display the Delete button but do something else you should look into UISwipeGestureRecognizer and handle it your self.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top