Shadow Of Shadow Of - 4 months ago 20
iOS Question

Handle deselect row in table view

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.

Answer

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.