Proper deselection in UITableView

It may not be obvious but the proper way to deselect a cell is this:

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow]
                                animated:YES];
}

And of course not in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath. And the reason is that when you return from the details page you see how the cell is being deselected, which hints you about your previous choice and just looks nice.

  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • LinkedIn
  • E-mail this story to a friend!
  • Print this article!

  • Chris Gummer

    That should be in viewWillAppear correct? UITableViewController implements it as such:

    http://developer.apple.com/library/ios/ipad/#documentation/UIKit/Reference/UITableViewController_Class/Reference/Reference.html

  • http://www.dimzzy.com/ dimzzy

    Hmm, looks like you are right. But actually I don't see any difference if it's called from 'will' or 'did' method. Actually it seems to me more logical to do it in 'did' method because you want to show user how selection fades away so it should be done after the table appears.

  • http://twitter.com/gcamp Guillaume Campagna

    It should in fact be in viewWillAppear:. That means that the deselection animation is happening during the navigation controller animation. In viewDidAppear:, the animation will happened *after* the navigation animation.

    It just looks better.

blog comments powered by Disqus