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

查看:14
本文介绍了选中时选中的 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 采用的方法,即保留选中的行,然后在视图重新出现时取消选择它(在控制器之后你're push 会从堆栈中弹出).

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];
}

正如我所说,如果您使用 UITableViewControllerUITableViewControllerviewWillAppear:em>没有看到这种行为,你应该检查你是否在你的类自己的 viewDidAppear: 中调用 super 实现.

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天全站免登陆