UITableView does not automatically deselect the selected row when the table re-appears

35,181

Solution 1

When your main ViewController is from type UITableViewController, it has a property clearsSelectionOnViewWillAppear, which is per default YES - so it will clear the selection automatically.

This property is not available for an UITableView, i guess it's because it has no ViewWillAppear method either.

A UIViewController doesn't need this property because it has no UITableView originally.

conclusion: you'll have to implement it by yourself when you do not use a UITableViewController.

Solution 2

Do the deselection in didSelectRowAtIndexPath instead of viewWillAppear:

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

Solution 3

In swift, you can add the following lines in your viewWillAppear

if let indexPath = tableView.indexPathForSelectedRow() {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

In swift 2, it's without parantheses:

if let indexPath = tableView.indexPathForSelectedRow {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

In Swift 4 (and 3?) the function name was cleaned up:

if let indexPath = tableView.indexPathForSelectedRow {
    tableView.deselectRow(at: indexPath, animated: true)
}

Solution 4

I dont think deselecting the selected row is automatic... I normally do it before pushing to the next view

- (void)tableView:(UITableView *)tableView 
        didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // to do other things
    [self.navigationController pushViewController:yourNextViewController animated:YES];
}

Solution 5

Nothing is wrong--deselecting the highlighted row is always "manual". If you look at Apple's sample code, you will see the same thing.

Share:
35,181
Besi
Author by

Besi

Updated on July 01, 2021

Comments

  • Besi
    Besi almost 3 years

    Normally a selected row in a UITableView gets deselected with an animation when the user pops back from the detail view.

    However, in my case where I have a UITableView embedded in a UIViewController I have to do it manually in viewWillAppear like so:

    -(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        // For some reason the tableview does not do it automatically
        [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow 
                                      animated:YES];  
    }
    

    Why is this and how to fix it?

  • Besi
    Besi over 11 years
    Yes it is automatic if the table is inside a UITableViewController See Rene's answer...
  • Besi
    Besi over 11 years
    Have a look at Rene's answer.
  • Mason
    Mason over 10 years
    The reason UITableView does not have a viewWillAppear method is because that is the controller's, rather than the view's responsibility. Since UITableView is a UIView subclass, it does not deal with this mechanism.
  • Rudolf Real
    Rudolf Real about 9 years
    [myTableView deselectRowAtIndexPath:[myTableView indexPathForSelectedRow] animated:YES];
  • Chris Conover
    Chris Conover almost 9 years
    Note that as of Xcode 6.3 / iOS 8.3, the IB / storyboard based setting appears to be broken as the setting on the backing TableViewController is always set to true / never false, despite deselecting in the XIB / Storyboard.
  • Hampden123
    Hampden123 over 8 years
    I like this solution the best because one doesn't need to store the selected indexPath. Thanks!!
  • mskw
    mskw about 8 years
    This is not good as when user comes back it doesn't know where they are coming from. It should anime the deselection when popping back to show user previous location.
  • billy
    billy over 7 years
    This is the only way to have the table view row's highlight fade away when the user goes back via sliding from the left edge of the screen
  • GeneCode
    GeneCode over 7 years
    This is clever. Have a +1.
  • Takagi
    Takagi over 6 years
    This is a terrible solution. When returning from yourNextViewController to the tableviewcontroller then there is no visual indication of what row was selected. It's better to do deselect in viewWillAppear.
  • meaning-matters
    meaning-matters about 6 years
    To follow iOS standard behaviour animated must be true.