如果这是以下问题的重复,我们深表歉意,如果是,请标记为:
how to set the data in second component based on first component selection in UIPickerview Not able to reload second compoenent based on value in first component in UIPickerview
我目前在UIPickerView
中有两个组件。我会在第二个组件的第3行被选中之前显示第一个组件,即Day(s)
。
我目前有以下简单的代码设置:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
if component == 0
{
return times_ARRAY[row]
}
else
{
return cycle_ARRAY[row]
}
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
if component == 0
{
return times_ARRAY.count
}
else
{
return cycle_ARRAY.count
}
}
我确定它显而易见,但我不确定如何应用简单的逻辑。
有人能指出我正确的方向吗?
由于
答案 0 :(得分:1)
声明一个全局变量daySelcted:Bool
并更改这两个委托方法。
var daySelcted:Bool = false
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0
{
if !daySelcted
{
return 0
}
return times_ARRAY.count
}
else
{
return cycle_ARRAY.count
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 1
{
if row == 2
{
daySelcted = true
}
else
{
daySelcted = false
}
pickerView.reloadComponent(0)
}
}