gpt4 book ai didi

swift - 如何将 UIPickerview selectRow 重置为默认零行

转载 作者:行者123 更新时间:2023-11-30 10:34:02 25 4
gpt4 key购买 nike

我有一个带有 UIPickerview 的应用程序,有 2 个组件,从 2 个数组获取数据。当选择组件 0 中的第 1 行时,我已在第 3 行设置了第二个组件。这很好,但我的问题是当组件 0 中的 row 发生更改时,我希望组件 1 重置为第 0 行(默认)。

@IBOutlet weak var myPicker: UIPickerView!

let firstArray = ["a", "b", "c", "d"]
let secondArray = ["A", "B", "C", "D"]

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

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
switch component {
case 0: return firstArray.count
case 1: return secondArray.count
default: break
}
return 0
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch component {
case 0: return firstArray[row]
case 1: return secondArray[row]
default: break
}
return ""
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

if myPicker.selectedRow(inComponent: 0) == 1 {
myPicker.selectRow(3, inComponent: 1, animated: true)
}
}

最佳答案

只需使用else if来选择组件中的第一个1为,

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

if myPicker.selectedRow(inComponent: 0) == 1 {
myPicker.selectRow(3, inComponent: 1, animated: true)
} else if component == 0 {
myPicker.selectRow(0, inComponent: 1, animated: true)
}
}

关于swift - 如何将 UIPickerview selectRow 重置为默认零行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58467513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com