Yeah, I too was having an issue trying to allow users to swipe away the Delete option. (Similar to how Apple's Mail app works)
The only way I found it to work as expected was if you also include a leadingSwipeAction... (If the user initially swipes either right or left they get different options). Once you add the other side, then users can swipe the Delete away.
Incidentally "Swipe the Delete Away" sounds like a dope emo song.
Here is code I currently have that is almost working perfectly. There is an occasional graphical anomaly with the leading button jumping to the right, but I think that might be due to some other code in my view. This code is borrowed heavily from https://chariotsolutions.com/blog/post/uitableview-swipe-actions-in-ios-11/
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if indexPath.section == Slider.Section.results.rawValue {
return true
} else {
return false
}
}
override func tableView(_ tableView: UITableView,
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let favoriteAction = self.contextualToggleFavoriteAction(forRowAtIndexPath: indexPath)
return UISwipeActionsConfiguration(actions: [favoriteAction])
}
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = UIContextualAction(style: .destructive,
title: "Delete") { (contextAction: UIContextualAction, sourceView: UIView, completionHandler: (Bool) -> Void) in
Filter.allMarkers.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
completionHandler(true)
}
let share = UIContextualAction(style: .normal,
title: "Share") { (contextAction: UIContextualAction, sourceView: UIView, completionHandler: (Bool) -> Void) in
print("About to share -> \(Filter.allMarkers[indexPath.row].title ?? "")")
completionHandler(true)
}
share.backgroundColor = Color.ocean
let swipeConfig = UISwipeActionsConfiguration(actions: [delete, share])
return swipeConfig
}
func contextualToggleFavoriteAction(forRowAtIndexPath indexPath: IndexPath) -> UIContextualAction {
let marker = Filter.allMarkers[indexPath.row]
let action = UIContextualAction(style: .normal,
title: "Favorite") { (contextAction: UIContextualAction, sourceView: UIView, completionHandler: (Bool) -> Void) in
if marker.toggleFavoriteFlag() {
completionHandler(true)
} else {
completionHandler(false)
}
}
//action.image = UIImage(named: "flag")
action.backgroundColor = marker.isFavorite ? Color.magenta : UIColor.gray
return action
}