选中时,选中的UItableViewCell保持蓝色 [英] Selected UItableViewCell staying blue when selected

查看:102
本文介绍了选中时,选中的UItableViewCell保持蓝色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在用户选择UITableView行后推送视图时,该行会获得蓝色突出显示,然后会出现新视图。没关系。但当我回来时,该行仍然以蓝色突出显示。这是我的didSelectRowAtIndexPath代码。

When I push a view after a user has selected a UITableView row, the row gets a blue highlight, and then the new view appears. That's fine. But when I go 'back' the row is still highlighted in blue. Here's my didSelectRowAtIndexPath code.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SettingsViewController *controller = [[SettingsViewController alloc] initWithNibName:@"SettingsView" bundle:nil];
    [[self navigationController] pushViewController:controller animated:YES];
    [controller release], controller = nil; 
}

我做错了什么?

推荐答案

如上面的答案所指出,你需要明确取消选择该行。关于如何执行此操作,您有两种选择。第一种,是在选择后立即取消选择行:

As the answers above point out, you need to explicitly deselect the row. You have two options as to how you do this. The first, is to deselect the row immediately after selection:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  ...
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

这样可以正常工作,但还有另一种方法,它的方法由 UITableViewController 采取,即选择该行,然后在视图重新出现时取消选择(在您推送的控制器弹出堆栈后)。

That will work just fine, but there is an alternative, and its the approach taken by UITableViewController which is to leave the row selected then deselect it when the view re-appears (after the controller you're pushing is popped off of the stack).

这有一个小优势,即用户在返回时可以看到他们之前选择的一瞥,这样他们就可以看到他们之前选择的内容。

This has the slight advantage that the user sees a glimpse of their previous selection when they return so they can see what they had selected previously.

要实现这一点,您只需要覆盖 viewWillAppear

To implement this, you just need to override viewWillAppear:

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

正如我所说,这是<$ c $的默认实施c> UITableViewController 的 viewWillAppear:如果您使用的是 UITableViewController 看到这种行为,你应该检查你是否在你的班级中调用 super 实现viewDidAppear:

As I said, this is what the default of implementation of UITableViewController's viewWillAppear: does so if you are using UITableViewController and not seeing this behaviour, you should check that you are calling the super implementation in your class' own viewDidAppear:.

更新(2013年10月30日):嗯,这是一个受欢迎的答案!正如Ben在评论中正确指出的那样,UITableViewController实际上是在 viewWillAppear中执行此操作:而不是 viewDidAppear: - 这是正确的定时。此外,使用UITableViewController的 clearsSelectionOnViewWillAppear 属性打开和关闭此行为。我已修改上面的答案以反映这一点。

Update (30 Oct 2013): well, this is a popular answer! As Ben rightly points out in the comments, UITableViewController actually does this in viewWillAppear: not viewDidAppear: - this is the correct timing. In addition, you turn this behaviour on and off using the clearsSelectionOnViewWillAppear property of UITableViewController. I've amended my answer above to reflect this.

这篇关于选中时,选中的UItableViewCell保持蓝色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆