Deselect Table Row with Animation

Here is a quick little trick to automatically deselect a table row once tapped by a user. With the approach below you can also adjust the length of the delay (in seconds) before the deselection occurs.

When working with a table, when a row is selected didSelectRowAtIndexPath is called, inside this method after you take care of any other business, call performSelector specifying a method to call and a delay. In the called method simply deselect the table row as shown below:

- (void) unselectCurrentRow
  // Animate the deselection
  [self.tableView deselectRowAtIndexPath:
      [self.tableView indexPathForSelectedRow] animated:YES];
- (void)tableView:(UITableView *)tableView 
      didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath
  // Any other table management you need
  // After one second, unselect the current row
  [self performSelector:@selector(unselectCurrentRow) 
      withObject:nil afterDelay:1.0];
  1. You might have some issue if the user presses the cell without releasing for more than one sec

  2. This assumes that the class owns the tableview, which may or may not be the case. I generally just perform [tableview deselectRowAtIndexPath:newIndexPath animated:YES]; in the didSelectRowAtIndexPath: method. Haven’t had a problem with that yet.

  3. Apple usually implements deselection in the viewWillAppear: method of the controller so that it deselects only when the pushed view controller pops. Of course in the case you’re using a navigation controller and your table view pushes view controllers.

    – (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];

Comments are closed.