简体   繁体   中英

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:

    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!" 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 ?

TLDR: i want swipe action callback to be triggered only when the button that appears is tapped.

Thank you.

To prevent this behavior you need an implementation of new 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

Remember tableView.isEditing need to be false to allow trailingSwipeActionsConfigurationForRowAt be called.

completionHandler(true) // passing true enable handler call action

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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