ios 我可以在 UIPickerView 中以编程方式滚动到所需的行吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4894705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I programmatically scroll to a desired row in UIPickerView?
提问by pradeepa
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 后突出显示第一行。如何以编程方式突出显示特定行或滚动到特定行?
回答by warrenm
As always, this is thoroughly documented. The Apple documentation for UIPickerView
should tell you that the method you probably want is – selectRow:inComponent:animated:
.
与往常一样,这有详尽的记录。Apple 的文档UIPickerView
应该告诉你你可能想要的方法是– selectRow:inComponent:animated:
.
回答by Thomás Calmon
If you want to trigger delegate's method pickerView:didSelectRow:inComponent, you should call it manually:
如果你想触发委托的方法pickerView:didSelectRow:inComponent,你应该手动调用它:
Obj-C
对象-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)
回答by Rich
Yes, it's very easy [picker selectRow:row inComponent:component animated:NO];
是的,这很容易 [picker selectRow:row inComponent:component animated:NO];
回答by Spanky
Working in iOS 9 with XCode 7.3, to make runtime changes to data in a picker view called fieldPicker:
在 iOS 9 中使用 XCode 7.3,在名为 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];
回答by Mitemmetim
Check if you call the method self.myPickerView.selectRow(0, inComponent: 0, animated: false)
afteryou added the pickerView as a subview.
self.myPickerView.selectRow(0, inComponent: 0, animated: false)
在将 pickerView 添加为子视图后检查是否调用了该方法。
First, I called the method beforeI added the pickerView as a subview and it did not work. Therefore, I called it afterwards and then it worked!
首先,我在将 pickerView 添加为子视图之前调用了该方法,但它不起作用。因此,我事后调用它然后它起作用了!
Probably pretty obvious and not my smartest move but still maybe someone has the same problem some day and I hope I could help! :)
可能很明显,不是我最聪明的举动,但也许有一天有人会遇到同样的问题,我希望我能帮上忙!:)