objective-c 加载 UIPickerView 后选择行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/812995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:28:30  来源:igfitidea点击:

Select row After UIPickerView is loaded

objective-ciphonecocoa-touchuipickerview

提问by Steve Gibson

I have an iPhone database app that loads a UIPickerViewwith data from a table. I want to set the selected row to a particular row from the data in another table.

我有一个 iPhone 数据库应用程序,UIPickerView可以从表中加载数据。我想将所选行设置为另一个表中数据的特定行。

For example: Let's say I have a UIPickerViewthat is loaded with X number of names of the iPhone users' friends (the number of names is variable; it could be 1 or 1000 and all are entered into the database by the user). The iPhone user has a preference set that their current best friend is TED. I want the UIPickerViewto be position to TEDwhen displayed.

例如:假设我有一个UIPickerView加载了 X 个 iPhone 用户朋友的名字(名字的数量是可变的;它可以是 1 或 1000,并且所有都由用户输入到数据库中)。iPhone 用户有一个偏好设置,即他们目前最好的朋友是TED。我希望在显示时UIPickerView定位到TED

Where do I call selectRow?

我在哪里打电话selectRow

I tried in viewDidAppear, but it was never called, in titleForRowwhich caused all kinds of strange behavior. viewDidLoadand viewWillAppearare out of the question, because I don't know what's in the datasource to the picker yet.

我尝试过viewDidAppear,但从未调用过titleForRow,导致各种奇怪的行为。viewDidLoad并且viewWillAppear是不可能的,因为我还不知道选择器的数据源中有什么。

回答by Jab

Call it after you retrieve the data. I don't know where you load the data so this is just a mockup of what should work. If this doesn't make sense, let me know where you are loading data so I can piece it together in my head :)

检索数据后调用它。我不知道你在哪里加载数据,所以这只是一个应该工作的模型。如果这没有意义,请告诉我您在哪里加载数据,以便我可以在脑海中将其拼凑起来:)

-(void)viewDidLoad
{
    // load data from sql
    // self.someDataArray = DATAFROMSQL
    [picker reloadAllComponents];
    [picker selectRow:0 inComponent:0 animated:YES];
}

回答by nspire

There is a common issue when a (default) UIPickerView item needs to be selected when the UIPickerView is initially shown. The problem is related to the sequence of events. From reading previous posts, it seems that the [pickerView selectRow:n] is normally called in the view controllers ViewDidLoad event. However, the UiPickerView data is only loaded after the ViewDidLoad event, which means any selection code in ViewDidLoad will have no effect. To remedy, place the selection code in ViewDidAppear;

当最初显示 UIPickerView 时需要选择(默认)UIPickerView 项时,存在一个常见问题。问题与事件的顺序有关。从阅读以前的帖子来看,似乎 [pickerView selectRow:n] 通常在视图控制器 ViewDidLoad 事件中调用。但是,UiPickerView 数据仅在 ViewDidLoad 事件之后加载,这意味着 ViewDidLoad 中的任何选择代码都将无效。为了补救,将选择代码放在 ViewDidAppear 中;

- (void) viewDidAppear:(BOOL)animated {
[m_pickerView selectRow:nSelectedItem inComponent:0 animated:YES]; }

回答by Alex Reynolds

Place this wherever you initialize the UIViewthat contains your UIPickerViewas a subview:

把它放在你初始化UIView包含你UIPickerView的子视图的任何地方:

[myPickerView selectRow:rowWithTedsName inComponent:columnWithNames animated:NO];

回答by Blake Watters

I have just found that on the 4.3 SDK's the component loading behaviors are different between the iPhone and the iPad. On the iPhone, I was about to invoke selectRow: right after initializing the view before or after adding to the subview hierarchy. On iPad however, the selectRow call yielded no results. I wound up using a performSelector:withObject:afterDelay: call to wait 0.1 seconds before firing selectRow:inComponent:animated:. This had predictable results on the iPhone and iPad.

我刚刚发现在 4.3 SDK 上,iPhone 和 iPad 之间的组件加载行为是不同的。在 iPhone 上,我正要在添加到子视图层次结构之前或之后初始化视图之后调用 selectRow: 。然而,在 iPad 上, selectRow 调用没有产生任何结果。我结束了使用 performSelector:withObject:afterDelay: 调用在触发 selectRow:inComponent:animated: 之前等待 0.1 秒。这在 iPhone 和 iPad 上产生了可预测的结果。

回答by Er. Vihar

For Swift it's simply:

对于 Swift,它很简单:

self.picker?.selectRow(0, inComponent: 0, animated: true)

回答by user1760527

If you want the selected row permanently highlighted use this

如果您希望选定的行永久突出显示,请使用此

myPickerView.subviews[0].subviews[0].subviews[2].backgroundColor = myPickerView.tintColor;