如何在第二个组件中的特定行开始之前不在UIPickerView的第一个组件中显示数据?

时间:2017-04-07 05:11:20

标签: ios swift swift3 uipickerview

如果这是以下问题的重复,我们深表歉意,如果是,请标记为:

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中的内容: enter image description here

我目前在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
    }
}

我确定它显而易见,但我不确定如何应用简单的逻辑。

有人能指出我正确的方向吗?

由于

1 个答案:

答案 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)

    }

}