2

I have implemented editActionsForRowAtIndexPath which works fine when I manage to swipe the cell, but the swiping gesture is not always recognized, I have to swipe many times until quite randomly it works. Any idea why that would be?

Here is my code:

//Implement custom actions on swipe
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?  {
    var deleteRowAction = UITableViewRowAction()
    var ignoreRowAction = UITableViewRowAction()
    var acceptRowAction = UITableViewRowAction()

    switch (tabSegmentControl.selectedIndex) {
    case 0:
        deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Remove" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
            let deleteMenu = UIAlertController(title: nil, message: "Do you really want to remove this friend?", preferredStyle: .ActionSheet)

            let deleteAction = UIAlertAction(title: "Remove", style: UIAlertActionStyle.Default, handler: nil)
            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)

            deleteMenu.addAction(deleteAction)
            deleteMenu.addAction(cancelAction)

            self.presentViewController(deleteMenu, animated: true, completion: nil)
        })
        return [deleteRowAction]

    case 1:

        if let friendR = friendRequests {

        let friendRequest = friendR[indexPath.row]

        deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Refuse" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
            let deleteMenu = UIAlertController(title: nil, message: "Do you really want to refuse this request?", preferredStyle: .ActionSheet)

            let deleteAction = UIAlertAction(title: "Remove", style: UIAlertActionStyle.Default, handler: {(alert: UIAlertAction!) in
                    self.friendActions(friendRequest.userId, command: "cancel", indexPath:indexPath)
                }
            )

            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)

            deleteMenu.addAction(deleteAction)
            deleteMenu.addAction(cancelAction)

            self.presentViewController(deleteMenu, animated: true, completion: nil)
        })

        ignoreRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Ignore" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
            let ignoreMenu = UIAlertController(title: nil, message: "Do you really want to ignore this request?", preferredStyle: .ActionSheet)

            let ignoreAction = UIAlertAction(title: "Ignore", style: UIAlertActionStyle.Default, handler: nil)
            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)

            ignoreMenu.addAction(ignoreAction)
            ignoreMenu.addAction(cancelAction)

            self.presentViewController(ignoreMenu, animated: true, completion: nil)
        })

        acceptRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Accept" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
            let acceptMenu = UIAlertController(title: nil, message: "Do you really want to accept this request?", preferredStyle: .ActionSheet)


            let acceptAction = UIAlertAction(title: "Ignore", style: UIAlertActionStyle.Default, handler: nil)
            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)

            acceptMenu.addAction(acceptAction)
            acceptMenu.addAction(cancelAction)

            self.presentViewController(acceptMenu, animated: true, completion: nil)
        })

        acceptRowAction.backgroundColor = UIColor(red: 0.298, green: 0.851, blue: 0.3922, alpha: 1.0);
        }

        return [acceptRowAction, ignoreRowAction, deleteRowAction]

    default:
        break
    }

    return [deleteRowAction]
}
Kalianey
  • 2,738
  • 6
  • 25
  • 43

1 Answers1

1

I ran into this issue today and found that it was due to a competing gesture recognizer. The way to tell if this is your issue is if the editing is only triggered on very fast swipes that are very straight and perpendicular, right to left.

This SO post also explains the issue:

Swipe to delete UITableViewCell very difficult to trigger

You can resolve it by removing the competing gesture recognizers, or prioritizing them by using UIGestureRecognizerDelegate method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

or

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
Community
  • 1
  • 1
Strobocop
  • 11
  • 1
  • 1
  • I edited your answer to properly format your code but since I know _absolutely nothing_ about iOS development _ don't know if the starting dashes were intentional or not - they very well might be a syntax error. You should probably review. – Mike G Oct 13 '15 at 19:12