Theo mặc định hàng đầu tiên được tô sáng sau khi khởi tạo UIPickerView. Làm cách nào để đánh dấu một hàng cụ thể hoặc cuộn đến hàng cụ thể theo lập trình?Tôi có thể lập trình cuộn đến hàng mong muốn trong UIPickerView không?
29
A
Trả lời
60
Như thường lệ, tài liệu này được ghi chép kỹ lưỡng. Tài liệu của Apple cho UIPickerView
phải cho bạn biết rằng phương pháp bạn có thể muốn là – selectRow:inComponent:animated:
.
33
Có, nó rất dễ dàng [picker selectRow:row inComponent:component animated:NO];
21
Nếu bạn muốn kích hoạt phương pháp pickerView đại biểu của: didSelectRow: inComponent, bạn nên gọi nó bằng tay:
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)
2
Làm việc trong iOS 9 với XCode 7.3, để thực hiện thay đổi thời gian chạy tới dữ liệu trong một cái nhìn picker gọi 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];
Nếu ai không thể tìm thấy nó trong Xamarin.iOS (MonoTouch) nó được gọi là 'Chọn() 'không' SelectRow()' như bạn sẽ nghĩ. –
Đối với tôi, gọi phương thức '- selectRow: inComponent: animated:' là không đủ, tôi phải gọi đại biểu '-pickerView: didSelectRow: inComponent:' theo cách lập trình. – fruechtemuesli
Phải; lập trình thiết lập hàng đã chọn không gọi phương thức ủy nhiệm. Nếu bạn muốn thực hiện hành động để phản hồi tất cả các thay đổi lựa chọn, bạn phải tự gọi nó. – warrenm