Why does uitableview cell remain highlighted?

What would cause a tableview cell to remain highlighted after being touched? I click the cell and can see it stays highlighted as a detail view is pushed. Once the detail view is popped, the cell is still 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];

}
I prefer calling the deselectRowAtIndexPath in my viewDidAppear, if select the row brings up a new view. –  notnoop Dec 3 '09 at 15:59
Actually that is not the right place to call it, really... try using an Apple app with tables (Contacts is a good one) and you'll see after you push out to a new screen, on return the cell is still highlighted briefly before being deselected. In theory I think you do not have to do any extra work to have it deselect right away on its own, so code in viewDidAppear should not be needed... –  Kendall Helmstetter Gelner Dec 3 '09 at 20:40
@Kendall, @4thSpace: Maybe my last comment was confusing as to who I was referring to, apologies for that. UITableViewController calls the -deselectRowAtIndexPath:animated: method on its tableView property from -viewDidAppear. However, if you have a table view in a UIViewController subclass, you should call -deselectRowAtIndexPath:animated: from -viewDidAppear yourself. :) –  Daniel Tull Dec 4 '09 at 12:19
In my subclass of UITableViewController it was actually an override of -viewWillAppear: that broke it. Adding a call to [super viewWillAppear:animated] got it working again. –  Ben Challenor Jul 6 '11 at 9:38
Since 3.2, the automatic deselection behaviour occurs if your UITableViewController's clearsSelectionOnViewWillAppear property is set to YES (which is the default), and you haven't prevented [super viewWillAppear] from being called. –  Defragged Oct 31 '11 at 10:26
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

 

http://stackoverflow.com/questions/1840614/why-does-uitableview-cell-remain-highlighted#comment1733845_1840757

你可能感兴趣的:(UITableView)