自动取消选择表视图单元格后弹出详细视图? [英] Automatic deselection of table view cell after popping detail view?

查看:142
本文介绍了自动取消选择表视图单元格后弹出详细视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经被这一段时间所困扰了。

I've been stumped by this for a while...

当您在Apple的iPhone手机应用程序中点击联系人的单元格(进入视图与联系人详细信息),并回到上一个视图,有一个简短的动画(从蓝色到白色),显示取消选择的单元格。这是Apple自己的应用程序中所有表视图的行为,也是根据他们的人机接口指南的推荐行为。

When you tap the cell of a contact in Apple's Phone app for the iPhone (entering the view with contact details) and tap back to the previous view, there is a brief animation (fading from blue to white) showing the deselection of the cell. This is the behavior with all table views in Apple's own apps, and also the recommended behavior according to their Human Interface Guidelines.

但是,在我自己的项目中无法复制此行为。当我从详细视图返回到<$ p $ c> UITableView 时,未选择任何单元格。

However, in my own project I've been having trouble replicating this behavior. None of the cells in my UITableView are selected when I return to it from a detail view.

在Apple的文档中的CoreDataBooks示例代码,它具有所需的单元格取消选择行为,并且它看起来像表视图获取行为自动(没有任何具体实现)。

I looked through the CoreDataBooks sample code in Apple's documentation, which has the desired cell deselection behavior, and it seems like the table view gets the behavior "automatically" (without any specific implementation).

我也尝试在这些非常类似的问题中实施解决方案:

I've also tried implementing the solutions in these very similar questions:

What deselect selected cell when navigationController pops a view?

iPhone UITableView单元格保持选中状态

返回时,UITableView不会选择行

但是我总是得到相同的结果 - 当返回到 UITableView 时,没有选择任何单元格。 (即使在视图控制器的 -viewWillAppear:中添加 [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:animated]; 方法。)

But I always get the same result -- none of the cells in the UITableView are selected when I return to it. (Even after adding [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:animated]; in the view controller's -viewWillAppear: method.)

在一个问题的注释中也建议有 [[self tableView] reloadData]; -viewWillAppear:可能导致单元格不会保持选中状态。但是CoreDataBooks做同样的事情,仍然有所需的行为(貌似),没有任何特定的代码。

It was also suggested in the comments of one question that having [[self tableView] reloadData]; in -viewWillAppear: may be causing the cells not to stay selected. But CoreDataBooks does the same thing and still has the desired behavior (seemingly) without any specific code.

有关如何解决问题的任何建议?提前感谢。

Any suggestions on how to resolve the problem? Thanks in advance.

在旁注中,我不太明白为什么要取消选择单元格的代码应该在 -viewWillAppear中实现: (而不是 -viewDidAppear:)。这不会导致一切都发生之前表视图显示在屏幕上?这可能只是因为我对视图的生命周期缺乏正确的理解,但任何澄清将是很好的。

On a side note, I don't quite understand why the code to deselect the cell should be implemented in -viewWillAppear: (rather than -viewDidAppear:). Wouldn't that cause everything to happen before the table view is displayed on screen? This is probably just due to lack of proper understanding of a view's life cycle on my part, but any clarifications would be nice. Thanks again.

推荐答案

您是否已经尝试了UITableViewController的clearsSelectionOnViewWillAppear属性(不仅仅是一个tableview而是一个tableviewcontroller)?如果将它设置为 NO ,它将使选择保持蓝色。

要获得所需的效果,您可能还需要添加一个呼叫

- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated

,永远保持蓝色。

Have you experimented with the clearsSelectionOnViewWillAppear property of a UITableViewController (not just a tableview but a tableviewcontroller)? If you set it to NO it will make the selection stay blue.
To get your desired effect,you will also probably need to add a call to
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
to ensure that it doesn't stay blue forever.

查看 UITableView文档开始。

// EDIT //
如评论下面,我们最后做的是移动调用 [[self tableview] reloadData]; viewDidAppear

我的猜测是CoreDataBooks有一个足够复杂的表(包括部分和书,部分和书),事件的时间发生了。

//EDIT// As noted in the comments below, what we finally ended up doing is moving the call to [[self tableview] reloadData]; to viewDidAppear.
My guess is that CoreDataBooks has a complex enough table (with sections and books and sections and books) that something is going on with the timing of events.

这篇关于自动取消选择表视图单元格后弹出详细视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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