【问题标题】:How to not display data in first component of UIPickerView until the start of specific row in second component?如何在 UIPickerView 的第一个组件中显示数据,直到第二个组件中特定行的开始?
【发布时间】:2017-04-07 05:11:20
【问题描述】:

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

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 中拥有的内容:

我的 UIPickerView 目前有 2 个组件。我会在选择第二个组件的第 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
    }
}

我确信这很明显,但我不确定如何应用简单的逻辑。

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

谢谢

【问题讨论】:

    标签: ios swift swift3 uipickerview


    【解决方案1】:

    声明一个全局变量daySelcted:Bool并更改这2个委托方法。

    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)
    
        }
    
    }
    

    【讨论】:

    • 这似乎部分工作,但是当我开始滚动组件一时,组件一中的所有行都消失了
    • 好的,我只需要将 row == 2 固定为 row >=2,因为当我在组件 2 中选择 row > 2 时,组件 1 消失了,这看起来对吗?
    • "我希望第一个组件在第二个组件的第 3 行被选中之前不显示,即天。"我以为你想在row == 2 时显示组件 0。改成row >= 2
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    相关资源
    最近更新 更多