How to unselect a uitableview cell when the user returns to the view controller
Swift 3.0
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let selectionIndexPath = self.tableView.indexPathForSelectedRow {
self.tableView.deselectRow(at: selectionIndexPath, animated: animated)
}
}
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)
}