Reputation: 2063
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.
Upvotes: 1
Views: 2344
Reputation: 146
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
Upvotes: 6