bitmoe
bitmoe

Reputation: 391

Override swipe gesture on UITableView

I have a UITableView. I have added to this table a UIGestureRecognizer that looks for a swipe on a cell, and enables editing on that table if it detects a swipe. When I swipe right, it does indeed enable editing on the table. However, when I swipe left, I get the default red delete button appear on the right side of the cell. How can I disable this default behavior, and make it so that if I swipe left OR right, I get editing on either way?

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    foldersTable.editing=YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIGestureRecognizer *recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [foldersTable addGestureRecognizer:recognizer];
    [recognizer release];

}

Upvotes: 5

Views: 4266

Answers (3)

CashLee李秉骏
CashLee李秉骏

Reputation: 1048

I am using another way to solve this problem.

NSMutableArray *tableViewGestures = [[NSMutableArray alloc]initWithArray:[self.tableView gestureRecognizers]];
[[tableViewGestures objectAtIndex:2] setDirection:UISwipeGestureRecognizerDirectionLeft];
NSLog(@"directions %@" , [tableViewGestures objectAtIndex:2] );

NSArray *newTableViewGestures = [[NSArray alloc]initWithArray:tableViewGestures];
[self.tableView setGestureRecognizers:newTableViewGestures];

I got the tableViewGestures NSArray and reset it.If I wanna use SwipeGestureRecognizerDirectionRigth,I just need to set the direction again.It's easy.Right?

Upvotes: 0

Marcelo Alves
Marcelo Alves

Reputation: 1846

Just return UITableViewCellEditingStyleNone in your tableView:editingStyleForRowAtIndexPath: method.

Upvotes: 3

Benjamin Mayo
Benjamin Mayo

Reputation: 6679

There is a direction property on UISwipeGestureRecognizer. You can set that to both right and left swipes:

recognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;

Upvotes: 3

Related Questions