我可以编程方式滚动到UIPickerView所需的行?

默认情况下,第一行在初始化UIPickerView后突出显示。 如何突出显示特定行或以编程方式滚动到特定行?

一如既往,这是彻底logging。 UIPickerView的Apple文档应该告诉你,你可能想要的方法是– selectRow:inComponent:animated:

是的,这非常简单[picker selectRow:row inComponent:component animated:NO];

如果你想触发委托的方法pickerView:didSelectRow:inComponent,你应该手动调用它:

OBJ-C

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

迅速

 self.myPickerView.selectRow(0, inComponent: 0, animated: false) self.pickerView(self.myPickerView, didSelectRow: 0, inComponent: 0) 

使用XCode 7.3在iOS 9中工作,在select器视图中对数据进行运行时更改,称为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];