0

I am using SWTableViewCell and I want to know the action that is triggered when the user swipe Left or Right.

Ahmed Labib
  • 105
  • 6

2 Answers2

4

you have to add Gesture Recognizer in you cellForRowAtIndexPath

 UISwipeGestureRecognizer* swRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedRight:)];
[swRight setDirection:UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:swRight];

UISwipeGestureRecognizer* swLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedLeft:)];
[swLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[cell addGestureRecognizer:swLeft];

and then its selector method

-(void)cellSwipedRight:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        // your code
    }
}

-(void)cellSwipedLeft:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        // your code
    }
}
Rahul Shirphule
  • 989
  • 3
  • 14
  • 36
  • as I said to @kb920 - This approach only recognize the `UIGestureRecognizerStateEnded` State. – Ahmed Labib May 19 '16 at 07:42
  • No... it recognise multiple state as I comment in your question `UIGestureRecognizerStateBegan` ,`UIGestureRecognizerStateCancelled` , `UIGestureRecognizerStateChanged`, `UIGestureRecognizerStateEnded` etc – Rahul Shirphule May 19 '16 at 07:47
  • It worked thank You and also found another solution for this [SWTableViewCell](https://github.com/CEWendel/SWTableViewCell) Library `- (void)swipeableTableViewCell:(SWTableViewCell *)cell scrollingToState:(SWCellState)state;` – Ahmed Labib May 19 '16 at 07:50
1

Try this code it will works for you:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwiped:)];
    [swipe setDirection:UISwipeGestureRecognizerDirectionRight];
    [cell addGestureRecognizer:swipe];

    cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row];


return cell;
    }


    - (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer 
    {
        if (gestureRecognizer.state == UIGestureRecognizerStateEnded) 
        {
            UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view;
            NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
            //..
        }
    }