Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I programmatically scroll to a desired row in UIPickerView?

By default the first row is highlighted after initializing the UIPickerView. How do i highlight a particular row or scroll to particular row programmatically?

like image 294
pradeepa Avatar asked Feb 04 '11 04:02

pradeepa


4 Answers

As always, this is thoroughly documented. The Apple documentation for UIPickerView should tell you that the method you probably want is – selectRow:inComponent:animated:.

like image 156
warrenm Avatar answered Sep 28 '22 03:09

warrenm


If you want to trigger delegate's method pickerView:didSelectRow:inComponent, you should call it manually:

Obj-C

[self.myPickerView selectRow:0 inComponent:0 animated:NO]; [self pickerView:self.myPickerView didSelectRow:4 inComponent:0]; 

Swift

self.myPickerView.selectRow(0, inComponent: 0, animated: false) self.pickerView(self.myPickerView, didSelectRow: 0, inComponent: 0) 
like image 24
Thomás Pereira Avatar answered Sep 28 '22 03:09

Thomás Pereira


Yes, it's very easy [picker selectRow:row inComponent:component animated:NO];

like image 27
Rich Avatar answered Sep 28 '22 02:09

Rich


Working in iOS 9 with XCode 7.3, to make runtime changes to data in a picker view called fieldPicker:

// first load your new data to your picker view's data source (not shown here...) then load the data into the picker view using it's delegate
    [self.fieldPicker reloadAllComponents];

    // now that the data is loaded, select a row in the picker view through it's delegate
    [self.fieldPicker selectRow:([self.theFields count] - 1) inComponent:0 animated:NO];

// retrieve the row selected in the picker view to use in setting data from your data source in textboxes etc.
    long row = (long)[self.fieldPicker selectedRowInComponent: 0];
like image 35
Spanky Avatar answered Sep 28 '22 03:09

Spanky