UIPickerView 报告不正确的行
UIPickerView reports an incorrect row
当我自上而下遍历此 UIPickerView 时,每个元素的行都被正确报告。然而,从下到上遍历 UIPickerView,当 "Choice 3" 被点击时,该行将自身重置为零,而实际上它应该是 2。我添加了视觉证明的标签——它闪烁“2”,然后自行重置如所述遍历 UIPickerView 时为零。显然,不能依赖于显示元素和实际行之间的关系是否准确会破坏事情。不知道为什么这不起作用...
class ViewController3: UIViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var picker: UIPickerView!
@IBOutlet weak var textLabel: UILabel!
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 4
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView,titleForRow row: Int,forComponent component: Int) -> String? {
textLabel.text = String(row)
return pickerValues[row]
}
let pickerValues = ["Choice 1", "Choice 2", "Choice 3","Choice 4"]
如果目标是每次用户转动选择器视图 "wheel" 时更改标签,请实现委托方法 pickerView(_:didSelectRow:inComponent:)
。
当我自上而下遍历此 UIPickerView 时,每个元素的行都被正确报告。然而,从下到上遍历 UIPickerView,当 "Choice 3" 被点击时,该行将自身重置为零,而实际上它应该是 2。我添加了视觉证明的标签——它闪烁“2”,然后自行重置如所述遍历 UIPickerView 时为零。显然,不能依赖于显示元素和实际行之间的关系是否准确会破坏事情。不知道为什么这不起作用...
class ViewController3: UIViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var picker: UIPickerView!
@IBOutlet weak var textLabel: UILabel!
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 4
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView,titleForRow row: Int,forComponent component: Int) -> String? {
textLabel.text = String(row)
return pickerValues[row]
}
let pickerValues = ["Choice 1", "Choice 2", "Choice 3","Choice 4"]
如果目标是每次用户转动选择器视图 "wheel" 时更改标签,请实现委托方法 pickerView(_:didSelectRow:inComponent:)
。