I am using SWTableViewCell and I want to know the action that is triggered when the user swipe Left or Right.
Asked
Active
Viewed 458 times
0
-
Check this http://stackoverflow.com/questions/6167756/how-to-detect-a-swipe-to-delete-gesture-in-a-customized-uitableviewcell – kb920 May 19 '16 at 07:23
-
This approach only recognize the `UIGestureRecognizerStateEnded` State – Ahmed Labib May 19 '16 at 07:39
-
@ahmedlabib it have multiple options for `UIGestureRecognizerState` as like began ,cancel ,change , ended etc – Rahul Shirphule May 19 '16 at 07:42
-
1did you try this - (void)swipeableTableViewCell:(SWTableViewCell *)cell scrollingToState:(SWCellState)state; method of "SWTableviewCell" – Wolverine May 19 '16 at 07:51
-
Thank You @wolverine this worked perfect for me – Ahmed Labib May 19 '16 at 07:53
2 Answers
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];
//..
}
}