更改UIPickerView Swift的数据

时间:2017-09-04 16:40:42

标签: swift uipickerview

我正在搜索,但我找不到我的问题的答案。 另一个选择器更改后,我的应用程序应该更改一个选择器的数据。

正如你所看到的,在titleForRow方法中,我想设置数据,我也知道这个方法只调用一次,但我不知道在哪里以及我应该怎么做。

我认为,在didSelectRow方法中我也可以设置数据,但是我没有一点也不应该这样做。

这是我的代码,我希望这有用。

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { 
    var x = 0 
    @IBOutlet weak var leaguePicker: UIPickerView! 
    @IBOutlet weak var clubPicker: UIPickerView! 
    let league = ["Bundesliga", "Premier League"] 
    let club = [["ManCity", "Arsenal"],["Bayern", "Dortmund"]] 

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

    func numberOfComponents(in picker: UIPickerView) -> Int { 
        return 1 
    } 

    func pickerView(_ picker: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
    { 
        switch (picker){ 
            case leaguePicker: 
                return league.count 
            case clubPicker: 
                return club.count 
            default: 
                return 1 
        } 
    } 

    func pickerView(_ picker: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
        switch (picker){ 
            case leaguePicker: 
                return league[row] 
            case clubPicker: 
                if (x==0){ 
                    print("titleForRow call.. x=0") 
                    return club[0][row] 
                    // this is just happening at the beginning
                } else { 
                    print("titleForRow call.. x=1") 
                    return club[1][row] 
                    // this does not happen 
                } 
            default: return "an error occurred" 
        } 
    } 

    func pickerView(_ picker: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
    { 
        if (picker == leaguePicker) {
             if (row==0){ 
                //clubPicker.selectRow(0, inComponent: 0, animated: true) 
                x=0 
            } else { 
            x=1 // this call is working
            //clubPicker.selectRow(0, inComponent: 1, animated: true) 
            } 
        } 
    picker.reloadAllComponents() 
    //this does not lead to call titleForRow 
    } 
}

1 个答案:

答案 0 :(得分:1)

当第一个选择器中的值发生变化时,您需要重新加载第二个选择器。

func pickerView(_ picker: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
    if (picker == leaguePicker) {
        x = row
        clubPicker.reloadAllComponents()
    }
}

您还需要修复numberOfRowsInComponent

func pickerView(_ picker: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
{ 
    switch (picker){ 
        case leaguePicker: 
            return league.count 
        case clubPicker: 
            return club[x].count 
        default: 
            return 1 
    } 
} 

您的titleForRow可以简化:

func pickerView(_ picker: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
    switch (picker){ 
        case leaguePicker: 
            return league[row] 
        case clubPicker: 
            return club[x][row] 
        default: return "an error occurred" 
    } 
} 

我还建议您将x变量重命名为更有用的内容,例如selectedLeague