Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For UITableView do I need to implement trailingSwipeActionsConfigurationForRowAt and/or editActionsForRowAt?

iOS11 introduced trailingSwipeActionsConfigurationForRowAt (and leading...). Does that mean I now have to implement

  1. the trailing/leading methods to control swiping for iOS11 AND
  2. 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.

like image 237
zkon Avatar asked Nov 02 '17 04:11

zkon


People also ask

How do I add custom actions to a U itableview row?

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.

How to implement Tableview in UITableView?

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.

What happens when the user swipes horizontally in the table view?

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.

How do I display actions in response to a swipe?

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.


1 Answers

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.

like image 75
PGDev Avatar answered Sep 17 '22 20:09

PGDev