How to unselect a uitableview cell when the user returns to the view controller
Solution 1:
If you using UITableViewController
subclass then just set property
self.clearsSelectionOnViewWillAppear = YES;
else in viewWillAppear
just call
NSIndexPath *indexPath = self.tableView.indexPathForSelectedRow;
if (indexPath) {
[self.tableView deselectRowAtIndexPath:indexPath animated:animated];
}
// MARK: - Swift 3
if let indexPath = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: indexPath, animated: true)
}
Solution 2:
Swift 3.0
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let selectionIndexPath = self.tableView.indexPathForSelectedRow {
self.tableView.deselectRow(at: selectionIndexPath, animated: animated)
}
}
Solution 3:
try
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Deselect the row which was tapped
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}