UITableView does not automatically deselect the selected row when the table re-appears
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
.
Do the deselection in didSelectRowAtIndexPath
instead of viewWillAppear
:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//show the second view..
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
In swift, you can add the following lines in your viewWillAppear
if let row = tableView.indexPathForSelectedRow() {
tableView.deselectRowAtIndexPath(row, animated: true)
}
In swift 2, it's without parantheses:
if let row = tableView.indexPathForSelectedRow {
tableView.deselectRowAtIndexPath(row, animated: true)
}
In Swift 4 (and 3?) the function name was cleaned up:
if let indexPath = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: indexPath, animated: true)
}