1

I can see this behavior in both deprecated UITableViewRowAction class and UISwipeActionsConfiguration class:

If you have allowsMultipleSelection property set to true and, let's say, you have 3 rows selected:

When you start swiping any row in the table for a RowAction the previously selected rows -- all 3 of them -- become unhighlighted, and the property indexPathsForSelectedRows drops to nil.

  • Does this behavior make sense?
  • Is there any 'deselecting' callback (because I'm displaying the number of selected rows)
  • What are possible workarounds to persist the array of selected rows?
rommex
  • 763
  • 1
  • 8
  • 21

1 Answers1

2

UITableView enters editing mode when you swipe a row in the table. This is your

'deselecting' callback

You can backup your selected rows on entering the mode and restore on exiting:

class ViewController: UITableViewController {

    var indexPathsForSelectedRows: [IndexPath]?

    override func setEditing(_ editing: Bool, animated: Bool) {
        if editing {
            indexPathsForSelectedRows = tableView.indexPathsForSelectedRows
        } else {
            indexPathsForSelectedRows?.forEach { tableView.selectRow(at: $0, animated: false, scrollPosition: .none) }
        }
        super.setEditing(editing, animated: animated)
    }
}

Also note that if you re-arrange/delete/insert rows during editing, you'll need to update your stored indexPathsForSelectedRows accordingly so you restore correct index paths.

Pavel Kozlov
  • 993
  • 6
  • 16
  • 1
    I'm pretty sure you must add super.setEditing(editing, animated) to the overridden function though – rommex Jul 31 '19 at 09:22
  • Also unfortunately neither override func setEditing(_:, :) nor override var isEditing { didSet { } } is called when the UITableView is entering/leaving the editing mode :( so this solution doesn't seem to be correct – rommex Jul 31 '19 at 14:32
  • Did you set your UITableView delegates? – Pavel Kozlov Jul 31 '19 at 16:59
  • The method setEditing() is not a part of a delegate protocol, why are you asking? – rommex Jul 31 '19 at 17:13
  • 1
    If you subclass UITableViewController then overriding setEditing (by the way calling super.setEditting() clears tableView.indexPathsForSelectedRows) should work. Otherwise you can implement willBeginEditingRowAt and didEndEditingRowAt appropriately in your UITableViewDelegate. – Pavel Kozlov Jul 31 '19 at 18:55
  • So as a belated update, since I'm still bumping into this task: neither isEditing nor setEditing can be used as 'callbacks' because they are not called when the editing mode changes (I subclassed UITableView and tried to used the didSet{} hook). But your suggestion with the delegate's methods willBeginEditing... didEndEditing... is working ok. – rommex Sep 09 '19 at 14:20