Search code examples
iosuitableviewdirectionuiswipegesturerecognizer

How to disable swipe in particular direction on UITableViewCell in iOS?


I have implemented swipe-to-delete option in my table view by adding the following two methods:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        //Action to delete
    }
}

This working well, and whenever I swipe on the cells (both left-to-right and right-to-left), a red color delete button appears well.

But I want to show the delete button only if, the user swipes from right-to-left direction. When the user swipes in left-to-right, I want to perform someother actions. Is it possible to findout the direction here?


Solution

  • Just add a swipe gesture recognizer to the table view for the direction you want to ignore.

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(emptyMethod:)];
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.tableView addGestureRecognizer:swipe];
    

    Then implement the emptyMethod.. this method won't do anything.

    - (void)emptyMethod {}
    

    Everytime you swipe left, the empty method will be called and does.. nothing.