UI选择器视图错误我尝试获取它来填充模型,

时间:2018-07-09 06:48:16

标签: swift uipickerview

我试图让第二个选择器显示我创建的数组,但是没有,而且我已经弄了好几个小时。有什么想法吗?

    class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

     return aircraft.count
}


@IBOutlet weak var aircraftType: UIPickerView!
@IBOutlet weak var model: UIPickerView!


let aircraft = ["Airbus", "Boeing", "Bombardier", "Embraer"]
let models = [ "Airbus": ["A300", "A320", "A330", "A350", "A380"], "Boeing": ["737", "747", "757/767", "777", "787"], "Bombardier": ["CRJ200", "CRJ700/900"], "Embraer": ["ERJ145", "ERJ170", "ERJ190"] ]



func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 2
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return aircraft [row]

}


func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}

1 个答案:

答案 0 :(得分:0)

请找到更新的代码。您应该在选择的飞机上重新加载pickerview component。请选择可选绑定

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            if component == 0 {
                return aircraft.count
            }
            else {
                return (models[aircraft[aircraftType.selectedRow(inComponent: 0)]]?.count)!
            }
        }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            return aircraft [row]
        }
        else {
            return models[aircraft[aircraftType.selectedRow(inComponent: 0)]]?[row]
        }
    }


    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == 0 {
            aircraftType.reloadComponent(1)
        }
    }