我可以在UIPickerView中以编程方式滚动到想要的行吗?

时间:2022-08-24 23:47:58

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

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

4 个解决方案

#1


59  

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

和往常一样,这被彻底地记录下来。UIPickerView的苹果文档应该告诉您,您可能需要的方法是- selectRow:inComponent:animated:。

#2


33  

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

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

#3


24  

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

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

Obj-C

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)

#4


2  

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

使用XCode 7.3在ios9中工作,对名为fieldPicker的picker视图中的数据进行运行时更改:

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

#1


59  

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

和往常一样,这被彻底地记录下来。UIPickerView的苹果文档应该告诉您,您可能需要的方法是- selectRow:inComponent:animated:。

#2


33  

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

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

#3


24  

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

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

Obj-C

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)

#4


2  

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

使用XCode 7.3在ios9中工作,对名为fieldPicker的picker视图中的数据进行运行时更改:

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