iOS11 introduced trailingSwipeActionsConfigurationForRowAt
(and leading...). Does that mean I now have to implement
editActionsForRowAt
for iOS10? How to get the simulator to run iOS10 simulations to see how my app behaves in the back level OS? Since everything is now iOS11, I'm not sure how my app will be in that version?
To clarify: 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.
Since iOS 8, Apple has provided us with the provision to add our own customized actions to a U ITableView’s rows using the U ITableViewDelegate method — t ableView (_:editActionsForRowAt:). Asks the delegate for the actions to display in response to a swipe in the specified row.
Usually when you need to implement tableview in your app you select a master-detail application: Were you get the project all the UITableView implementation ready for you to start adapting in your application,but sometimes you need just a tableView either to display the configuration of your app or to display some other kind of information.
When the user swipes horizontally in a row, the table view moves the row content aside to reveal your actions. Tapping one of the action buttons executes the handler block stored with the action object.
Asks the delegate for the actions to display in response to a swipe in the specified row. Use this method when you want to provide custom actions for one of your table rows. When the user swipes horizontally in a row, the table view moves the row content aside to reveal your actions.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With