For UITableView do I need to implement trailingSwipeActionsConfigurationForRowAt and/or editActionsForRowAt?
According to your requirement:
I want actions for rows, but I don't want the default behaviour in iOS11 of performsFirstActionWithFullSwipe. If I just implement editActionsForRowAt then iOS11 does the full swipe.
In iOS-10 and below,
to get the edit actions
work in a UITableView
, just implement the below methods:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
{
return true
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
{
let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexpath) in
//YOUR_CODE_HERE
}
deleteAction.backgroundColor = .red
return [deleteAction]
}
In iOS-11,
2 new methods were introduced to support editing in a UITableView
, i.e. leadingSwipeActionsConfigurationForRowAt
and trailingSwipeActionsConfigurationForRowAt
.
According to Apple,
Swipe actions
These methods supersede -editActionsForRowAtIndexPath: if implemented
return nil to get the default swipe actions
So, you can implement these 2 methods to get the iOS-11 specific behaviour. Even if you don't editActionsForRowAt
will be called.
If you don't want the default full swipe behaviour of edit action
in iOS-11, just set performsFirstActionWithFullSwipe to false
.
Example:
@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
return nil
}
@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, view, handler) in
//YOUR_CODE_HERE
}
deleteAction.backgroundColor = .red
let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
configuration.performsFirstActionWithFullSwipe = false
return configuration
}
Let me know if you still face any issues.
If you want some form of row actions, you need to:
Implement just editActionsForRowAt
and it will work for iOS 10, 11, 12 and 13. But it's NOT available in iOS 14 (or newer).
or:
Implement editActionsForRowAt
on iOS 10, and implement the trailing/swiping methods on iOS 11 or newer - which are slightly fancier.
or:
Ignore iOS 10 and only support iOS 11 or newer trailing/swiping actions since most of your customers will likely be running iOS 11 anyway - and row actions are generally considered an optional feature anyway.
If you don't want the full swipe behavior, you can only achieve this on iOS 11 or newer where you can set performsFirstActionWithFullSwipe
to false
Full swipe methods aren't available until iOS 11. They won't work in iOS 10.