如何在UIPickerView中显示所需的行?

时间:2019-03-28 16:23:52

标签: arrays swift uipickerview

我知道数组中的数据,加载我的应用程序时需要显示选定的行。例如,如何在开始时选择“四个”?

enter image description here

let arrayOfNumbers = ["One", "Two", "Three", "Four", "Five"]

override func viewDidLoad() {
    super.viewDidLoad()
    pickerTest.delegate = self
    pickerTest.dataSource = self
}

@IBOutlet weak var pickerTest: UIPickerView!

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return arrayOfNumbers.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return arrayOfNumbers[row]
}

1 个答案:

答案 0 :(得分:0)

pickerTest.selectRow(4, inComponent: 0, animated: true)