如何最初在UITableView中选择一行 [英] How to initially select a row in UITableView
问题描述
我找不到合适的位置来呼叫selectRowAtIndexPath:animated:scrollPosition:
来最初在UITableView
中选择一行.初始化表视图控制器时,表数据尚未加载,因此我无法在UITableViewController
初始化后立即进行选择(否则将发生越界异常).
I can not find a good spot to call selectRowAtIndexPath:animated:scrollPosition:
to initially select a row in UITableView
. Table data have not been loaded when the table view controller is initialized, so I can't do selection immediate after the initialization of my UITableViewController
(Over bound exception would occur otherwise).
推荐答案
即使视图以前已经显示过,您也需要使用viewWillAppear:
设置视图的状态,直到视图每次出现. UITabViewController或UINavigationViewController中的任何不可见视图都可以随时卸载,因此您不能依靠不可见视图来保持其状态.
You need to use viewWillAppear:
to set the state of a view before it appears, every time it appears, even if the view has already been displayed previously. Any non-visible view in a UITabViewController or UINavigationViewController can be unloaded at any time, so you cannot count on a non-visble view to retain its state.
或者,为了更好地控制,请实现loadView
,viewDidLoad
和viewDidUnload
.
Or, for finer control, implement loadView
, viewDidLoad
, and viewDidUnload
.
如果要维护自己的视图控制器层次结构,则必须手动将viewWillAppear,viewWillDisappear等消息转发到子视图控制器.
If you are maintaining your own hierarchy of view controllers you will have to manually forward the viewWillAppear, viewWillDisappear, etc., messages to your sub-view controllers.
这篇关于如何最初在UITableView中选择一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!