Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selected UItableViewCell staying blue when selected

When I push a view after a user has selected a UITableView row, the row gets a blue highlight, and then the new view appears. That's fine. But when I go 'back' the row is still highlighted in blue. Here's my didSelectRowAtIndexPath code.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SettingsViewController *controller = [[SettingsViewController alloc] initWithNibName:@"SettingsView" bundle:nil];
    [[self navigationController] pushViewController:controller animated:YES];
    [controller release], controller = nil; 
}

What am I doing wrong?

like image 506
cannyboy Avatar asked May 10 '10 13:05

cannyboy


4 Answers

As the answers above point out, you need to explicitly deselect the row. You have two options as to how you do this. The first, is to deselect the row immediately after selection:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  ...
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

That will work just fine, but there is an alternative, and its the approach taken by UITableViewController which is to leave the row selected then deselect it when the view re-appears (after the controller you're pushing is popped off of the stack).

This has the slight advantage that the user sees a glimpse of their previous selection when they return so they can see what they had selected previously.

To implement this, you just need to override viewWillAppear:

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

As I said, this is what the default of implementation of UITableViewController's viewWillAppear: does so if you are using UITableViewController and not seeing this behaviour, you should check that you are calling the super implementation in your class' own viewDidAppear:.

Update (30 Oct 2013): well, this is a popular answer! As Ben rightly points out in the comments, UITableViewController actually does this in viewWillAppear: not viewDidAppear: - this is the correct timing. In addition, you turn this behaviour on and off using the clearsSelectionOnViewWillAppear property of UITableViewController. I've amended my answer above to reflect this.

like image 77
Luke Redpath Avatar answered Oct 23 '22 22:10

Luke Redpath


UITableViewController has a BOOL property called clearsSelectionOnViewWillAppear which does exactly what you want.

By default it is set to YES, but I've noticed that you can (sometimes accidentally) disable this property by implementing your own viewWillAppear: method. I think this is because the deselection happens during [UITableViewController viewWillAppear:] which might never get called if you override it.

The solution is easy, then. Just call super's version of viewWillAppear: somewhere in your version of that method:

- (void)viewWillAppear:(BOOL)animated {
  // Your custom code.
  [super viewWillAppear:animated];
}

Apple recommends always calling the super version of any of the view{Did,Will}{A,Disa}ppear methods if you override them.

References

  • UITableViewController Class Reference (for clearsSelectionOnViewWillAppear)
  • UIViewController Class Reference (for viewWillAppear:)
like image 45
Tyler Avatar answered Oct 23 '22 20:10

Tyler


You need to deselect it:

[tableView deselectRowAtIndexPath:indexPath animated:YES];

like image 20
pkananen Avatar answered Oct 23 '22 20:10

pkananen


If your controller is based on UITableViewController, you get this feature for free. But I often ended up using UIViewController with no. of other controls in addition to UITableView, in that case, you should override your viewWillAppear to

-(void) viewWillAppear:(BOOL)animated{
    // Unselect the selected row if any
    NSIndexPath*    selection = [devListTableview indexPathForSelectedRow];
    if (selection){
        [tableview deselectRowAtIndexPath:selection animated:YES];
    }
}
like image 4
palaniraja Avatar answered Oct 23 '22 21:10

palaniraja