简体   繁体   English

UITableViewRowAction操作是在滑动而不是在点击时触发的

[英]UITableViewRowAction action is triggered on swipe instead on tap

I am not sure i understood it correctly, the documentation is a bit confusing here, but given the following code in the body of a UITableView delegate: 我不确定我是否正确理解,这里的文档有点混乱,但是在UITableView委托的主体中给出了以下代码:

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

        let action = UITableViewRowAction(style: UITableViewRowAction.Style.normal,
                                          title: "Do Something!") { (_, indexPath) in
                                            self.doSomething()
                                            }
        return [action]
    }

the call to doSomething() method is performed in simulator on completion of the swipe-to-left action beside on tap on the "Do Something!" 调用doSomething()方法的操作是在模拟器中完成“向左滑动”操作后,点击“做某事!”之后执行的 button. 按钮。 I don't want the call to be performed twice. 我不希望通话被执行两次。 Is there something wrong with my configuration or i have not understood the purpose of UITableViewRowAction ? 我的配置有问题吗?还是我不了解UITableViewRowAction的目的?

TLDR: i want swipe action callback to be triggered only when the button that appears is tapped. TLDR:我希望在点击出现的按钮时才触发滑动动作回调。

Thank you. 谢谢。

To prevent this behavior you need an implementation of new iOS. 为了防止这种行为,您需要实现新的iOS。

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let myAction = UIContextualAction(style: .someStyle, title: "Some tilte") { (action, sourceView, completionHandler) in
    print("action has been triggered")
    completionHandler(true)
}
let preventSwipeFullAction = UISwipeActionsConfiguration(actions: [myAction ])
preventSwipeFullAction .performsFirstActionWithFullSwipe = false // set false to disable full swipe action
return preventSwipeFullAction 
}

TableView Trailing Swipe Action TableView尾随滑动动作

Remember tableView.isEditing need to be false to allow trailingSwipeActionsConfigurationForRowAt be called. 记住tableView.isEditingfalse以允许trailingSwipeActionsConfigurationForRowAt被调用。

completionHandler(true) // passing true enable handler call action completionHandler(true) //传递true启用处理程序调用操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM