UIPickerView报告错误的行

时间:2018-04-29 16:05:51

标签: ios swift uipickerview

当我从上到下遍历此UIPickerView时,会正确报告每个元素的行。但是,当"选择3"时,从底部到顶部遍历UIPickerView。被轻拍,行重置为零,它应该实际为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"]

1 个答案:

答案 0 :(得分:0)

如果目标是每次用户转动"转动标签时更改标签。在选择器视图中,实现委托方法pickerView(_:didSelectRow:inComponent:)