UITableView does not automatically deselect the selected row when the table re-appears
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.
Besi
Updated on July 01, 2021Comments
-
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 aUIViewController
I have to do it manually inviewWillAppear
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 over 11 yearsYes it is automatic if the table is inside a UITableViewController See Rene's answer...
-
Besi over 11 yearsHave a look at Rene's answer.
-
Mason over 10 yearsThe reason
UITableView
does not have aviewWillAppear
method is because that is the controller's, rather than the view's responsibility. SinceUITableView
is a UIView subclass, it does not deal with this mechanism. -
Rudolf Real about 9 years
[myTableView deselectRowAtIndexPath:[myTableView indexPathForSelectedRow] animated:YES];
-
Chris Conover almost 9 yearsNote 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 over 8 yearsI like this solution the best because one doesn't need to store the selected indexPath. Thanks!!
-
mskw about 8 yearsThis 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 over 7 yearsThis 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 over 7 yearsThis is clever. Have a +1.
-
Takagi over 6 yearsThis 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 inviewWillAppear
. -
meaning-matters about 6 yearsTo follow iOS standard behaviour
animated
must betrue
.