我正在使用 Xcode 13.1 版
我使用 UIPickerView,但是当我选择值它跳转到另一个时,我有 10 个项目[item 0 ... item 9]
,当我选择项目 1时值跳转到 6,我该如何解决?
这里我选择“Item 1”,但 Xcode 选择并打印 6 而不是 1
我的代码:
import UIKit
class LengthViewController: UIViewController {
@IBOutlet weak var pickerView: UIPickerView!
let pickerData = ["Item 0","Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8","Item 9"]
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
}
}
extension LengthViewController: UIPickerViewDelegate{
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
print(row)
return pickerData[row]
}
}
extension LengthViewController: UIPickerViewDataSource{
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
}