【问题标题】:IOS UITableViewRowAction: swipe on cell works randomlyIOS UITableViewRowAction:在单元格上滑动随机工作
【发布时间】:2015-07-25 11:43:43
【问题描述】:

我已经实现了editActionsForRowAtIndexPath,当我设法滑动单元格时它工作得很好,但是滑动手势并不总是被识别,我必须多次滑动直到它非常随机地工作。知道为什么会这样吗?

这是我的代码:

//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]
}

【问题讨论】:

    标签: ios swift swipe uitableviewrowaction


    【解决方案1】:

    我今天遇到了这个问题,发现这是由于一个竞争的手势识别器造成的。判断这是否是您的问题的方法是,编辑是否仅在非常笔直且垂直(从右到左)非常快速的滑动时触发。

    这篇 SO 帖子也解释了这个问题:

    Swipe to delete UITableViewCell very difficult to trigger

    您可以通过删除竞争手势识别器来解决它,或者使用 UIGestureRecognizerDelegate 方法对其进行优先排序:

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

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

    【讨论】:

    • 我编辑了您的答案以正确格式化您的代码,但由于我对 iOS 开发一无所知 _ 不知道开头的破折号是否是故意的 - 他们很好可能是语法错误。你应该回顾一下。
    猜你喜欢
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多