UITableView gets deselected at swpie action

Hi,

I'm using a UITableViewController to sellect multiple cells.

tableView.allowsMultipleSelection = true
tableView.allowsSelectionDuringEditing = true


This works fine until the user makes a swipe action. A the moment the swipe action start all TableViewCells get deselected. Is there a way to keep selection state?

Could you show how you implemented the swipe action (the one for delete ?)


Could you save the array of selected cells before beginning swipe and restore at the end of swipe ?

    override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        
        let attendee = attendees[indexPath.row]
        
        let action = UIContextualAction(style: .normal, title: "Test") { [weak self] (action, view, handler) in
            let messageViewController = MessageComposerViewController()
            messageViewController.recipients = [attendee.participant]
            messageViewController.message = Message(subject: "", message: "")
            messageViewController.completion = {
                self?.navigationController?.popToRootViewController(animated: true)
            }
            self?.navigationController?.pushViewController(messageViewController, animated: true)
            handler(true)
        }
        return UISwipeActionsConfiguration(actions: [action])
    }

i also tried func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? with the same result

If I remember well, when you return from the messageViewController, you reload the table.


But you can use state restoration :


State Preservation

If you assign a value to a table view’s

restorationIdentifier
property, it attempts to preserve the currently selected rows and the first visible row. The table’s data source may adopt the
UIDataSourceModelAssociation
protocol, which provides a way to identify a row’s contents independent of that row’s position in the table. If the table’s data source adopts the
UIDataSourceModelAssociation
protocol, the data source will be consulted when saving state to convert the index paths for the top visible row and any selected cells to identifiers. During restoration, the data source will be consulted to convert those identifiers back to index paths and reestablish the top visible row, and reselect the cells. If the table’s data source does not implement the
UIDataSourceModelAssociation
protocol, the scroll position will be saved and restored directly, as will the index paths for selected cells.

For more information about how state preservation and restoration works, see App Programming Guide for iOS.

Did anyone ever find a solution to this problem?

I found a solution on stack overflow that suggests that one may override setEditing(_:animated:) on the table view to get this behavior, but I have yet to find success. In my case, setEditing is not being called when a swipe action is being performed.

UITableView gets deselected at swpie action
 
 
Q