Search code examples
iosswiftuitableviewgesturedelete-row

UITableView: "didDeselectCell" method not called, although cell gets deselected


I use UITableView with enabled "swipe to delete" animation.

I need to monitor didDeselectCell method to enable/disable some controls on the app.

However, if the user initiates "swipe to delete" gesture, opens the "delete" button and then reverses the gesture (without completion), the cell gets deselected and no event is being registered.

Two questions:

  1. Is it possible to enable didDeselectCell event to be triggered in this case as well?
  2. How is it possible to get notified, that the cell got automatically deselected via "swipe to delete" ?

Solution

  • didDeselectRowAt (and willDeselectRowAt) are only called if the user taps to select a row while another row is already selected.

    NOT called when editing begins, or when deselecting / selecting via code with .deselectRow(at: ... or .selectRow(at: ... etc.

    One option would be to implement willBeginEditingRowAt:

    func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
        if let iPath = tableView.indexPathForSelectedRow {
            print("the row at \(iPath) was selected...")
            // do stuff for that row
        }
    }
    

    Depending on what all you need to do, this may or may not work for your case.