作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有 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/
我是一名优秀的程序员,十分优秀!