3

I have a UITableView in the Master of a Master-Detail setup. Users can interact with the cell through UITableViewRowAction, or search using a given search bar.

When the user interacts with the search bar or slides the cell to the left, the cell is deselected. I need to be notified when a cell is deselected.

I tried func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) but it does not seem to respond to situations like these.

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        print("Cell Deselected")
}

This never executed on the conditions I've given above, but it does get executed only when a different item in the tableView is selected, which is not what I need.

Berry
  • 2,143
  • 4
  • 23
  • 46

2 Answers2

0

Newer answer:

You'll need to catch when other responders are selected outside of the table.

For example, for going into edit mode:

func setEditing(_ editing: Bool, animated: Bool) {
    print("Cell deselected")
}

and going into the search bar, use this delegate method:

func searchBarShouldBeginEditing(UISearchBar)
{
     print("Cell Deselected")
}

Original answer:

Make sure you set your view controller (or whatever object your didDeselectRowAt function lives in) as the table view's delegate. You can set that in your storyboard or you can do it programatically.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • The delegate is set correctly. The deselect only gets called when you select a different item, but not when you perform actions like reloadData() or slide with `UITableViewRowAction` – Berry Jun 13 '17 at 03:49
0

You can keep the SelectedIndexPath on the class level and when the user touches anywhere outside the table you can call this method like this:-

tableView.deselectRow(at: SelectedIndexPath, animated: true)

You can also receive call from:

func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
Salman Ghumsani
  • 3,647
  • 2
  • 21
  • 34