Select row After UIPickerView is loaded

Objective CIphoneCocoa TouchUipickerview

Objective C Problem Overview


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

For example: Let's say I have a UIPickerView that 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 UIPickerView to be position to TED when displayed.

Where do I call selectRow?

I tried in viewDidAppear, but it was never called, in titleForRow which caused all kinds of strange behavior. viewDidLoad and viewWillAppear are out of the question, because I don't know what's in the datasource to the picker yet.

Objective C Solutions


Solution 1 - Objective C

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

Solution 2 - Objective C

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;

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

Solution 3 - Objective C

Place this wherever you initialize the UIView that contains your UIPickerView as a subview:

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

Solution 4 - Objective C

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.

Solution 5 - Objective C

For Swift it's simply:

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

Solution 6 - Objective C

If you want the selected row permanently highlighted use this

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteve GibsonView Question on Stackoverflow
Solution 1 - Objective CJabView Answer on Stackoverflow
Solution 2 - Objective CnspireView Answer on Stackoverflow
Solution 3 - Objective CAlex ReynoldsView Answer on Stackoverflow
Solution 4 - Objective CBlake WattersView Answer on Stackoverflow
Solution 5 - Objective CEr. ViharView Answer on Stackoverflow
Solution 6 - Objective Cuser1760527View Answer on Stackoverflow