如何根据第一个pickerView的选择更改第二个pickerView的数组

时间:2019-07-17 13:51:37

标签: swift xcode pickerview

我有两个pickerView,即pickerView1 = car_makepickerView2 = car_model

如果我选择car_make = Kia,我希望pickerView2显示模型数组[Rio, Venga, Ceed],如果我从pickerView1中选择了car_make=Ford,那么我希望pickerView2显示模型数组{{ 1}}。

我已经尝试编辑[Mondeo, Ka, Focus]函数,并且已经完成管理

didSelectRow

但是我不知道如何将新数组分配给carModelPicker

1 个答案:

答案 0 :(得分:2)

您需要

struct Car {
    let name:String
    let models:[String]
}

然后像这样填充数组

cars = [Car(name: "ppp", models: ["1","2","3"])]

对于picker1的数字行为cars.count,对于picker2的数字行为cars[selected].models.count

哪里

var selected = 0 // suppose you will show models of first car initially 

当用户点击didSelect

if pickerView == picker1 {
   selected = row
   picker2.reloadAllComponents()  
}