iosuitableviewuikituitableviewrowactionuiswipeactionsconfiguration

UITableView: swiping RowAction cancels the selected rows


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.


Solution

  • 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.