Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does UITableViewCell remain highlighted?

In your didSelectRowAtIndexPath you need to call deselectRowAtIndexPath to deselect the cell.

So whatever else you are doing in didSelectRowAtIndexPath you just have it call deselectRowAtIndexPath as well.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 // Do some stuff when the row is selected
 [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

The most clean way to do it is on viewWillAppear:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // Unselect the selected row if any
    NSIndexPath*    selection = [self.tableView indexPathForSelectedRow];
    if (selection) {
        [self.tableView deselectRowAtIndexPath:selection animated:YES];
    }
}

This way you have the animation of fading out the selection when you return to the controller, as it should be.

Taken from http://forums.macrumors.com/showthread.php?t=577677

Swift version

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    // deselect the selected row if any
    let selectedRow: IndexPath? = tableView.indexPathForSelectedRow
    if let selectedRowNotNill = selectedRow {
        tableView.deselectRow(at: selectedRowNotNill, animated: true)
    }
}

For the Swift users, add this to your code:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

It's paulthenerd's answer but in Swift instead of Obj-C.


Did you subclass -(void)viewWillAppear:(BOOL)animated? The selected UITableViewCell won't deselect when you don't call [super viewWillAppear:animated]; in your custom method.