1

I want to handle case when user deselect row (that already selected) by tap on this row. It is well known that tableView:willDeselectRowAtIndexPath: / tableView:didDeselectRowAtIndexPath: delegate methods not called in this case: they are called only if you tap on other, unselected yet row (my table view have single selection mode).

tableView:didSelectRowAtIndexPath: method also not called when I am deselecting row.

Is there is any easy solutions?

Update

The problem was in tableView:willSelectRowAtIndexPath: method, where I return nil in some cases, thats why tableView:didSelectRowAtIndexPath: didn't call. Thanks @Nekto for useful information and helping.

Yury
  • 6,044
  • 3
  • 19
  • 41

3 Answers3

1

I have such use case. Why you don't create a variable for lastSelectedRow(here I have problem if it was NSIndexPath and I used two integers) and on the method for selecting row just compare currentSelectedRow with lastSelectedRow.

Bhumika
  • 876
  • 7
  • 20
m1sh0
  • 2,236
  • 1
  • 16
  • 21
1

You can still use tableView:didSelectRowAtIndexPath, just use it like a check mark system would work.

The answer here explains how to use a checkmark. You can use this, or use the answer here.

Community
  • 1
  • 1
Jacolack
  • 1,365
  • 2
  • 11
  • 25
  • didSelectRowAtIndexPath not called – Yury Jul 19 '16 at 17:43
  • That is because you have already selected it... I highly recommend using the checkmark. – Jacolack Jul 19 '16 at 17:47
  • But at link you provided they use didSelectRowAtIndexPath method. Am I wrong? – Yury Jul 19 '16 at 17:50
  • Wait it is never being called for you? If so there is a problem with your code – Jacolack Jul 19 '16 at 17:51
  • didSelectRowAtIndexPath called when I select new, unselected row. didDeselectRowAtIndexPath called, when I deselect row by selecting other row. None of this methods called when I deselect row by tapping on selected row – Yury Jul 19 '16 at 17:55
  • 1
    I fully understand now, and I have an idea... Instead of isSelected, change the background color but still deselectRowAtIndexPath, then didSelectRowAtIndexPath will still be called, and then you can change the background color back to white or whatever it was originally. – Jacolack Jul 19 '16 at 18:00
1

Apple docs say next about - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

This method isn’t called when the editing property of the table is set to YES (that is, the table view is in editing mode).

That means that this method should be called unless your code has a bug or you haven't updated delegate of your table view or table is in editing mode.

One of the possible problems could be that you have incorrectly implemented - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath and are returning nil for the selected row. In that case didSelect... isn't called:

Return nil if you don’t want the row deselected.

Alternatively you can implement another table view delegate method: - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath:

Tells the delegate that the highlight was removed from the row at the specified index path.

See more about managing selections in table views here.

Nekto
  • 17,837
  • 1
  • 55
  • 65