[ACCEPTED]-Select row After UIPickerView is loaded-uipickerview

Accepted answer
Score: 142

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

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

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

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

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

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

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

Score: 4

For Swift it's simply:

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

0

Score: 0

If you want the selected row permanently 1 highlighted use this

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

More Related questions