码迷,mamicode.com
首页 > 其他好文 > 详细

Why does uitableview cell remain highlighted?

时间:2014-07-12 00:07:16      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   

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

Why does uitableview cell remain highlighted?,布布扣,bubuko.com

Why does uitableview cell remain highlighted?

标签:des   style   blog   http   color   os   

原文地址:http://www.cnblogs.com/savagemorgan/p/3832879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!