0

I have two pickerViews i.e. pickerView1 = car_make, pickerView2 = car_model.

If I select car_make = Kia, I want pickerView2 to show the array of models [Rio, Venga, Ceed], and if I selected car_make=Ford from pickerView1, then I want pickerView2 to show the model array [Mondeo, Ka, Focus].

I've tried editing the didSelectRow function, and have managed

if pickerView == carMakePicker {
    if carMake[row] == "Kia"{
        // How to assign new array?
    }
}

but then I don't know how to assign the new array to carModelPicker

BrudZ
  • 40
  • 5
  • Can you show us a bit more of your code? What's in your dataSource? How do you store the car brands and the car models? – 108g Jul 17 '19 at 13:55

1 Answers1

2

You need

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

Then fill the array like

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

For number rows of picker1 be cars.count and for picker2 be cars[selected].models.count

Where

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

When the user clicks didSelect do

if pickerView == picker1 {
   selected = row
   picker2.reloadAllComponents()  
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Thank you so much! That makes so much more sense and now works. The only thing now is when I select a different car_Make, pickerView2 now focusses on the bottom entry rather than the top, but that's the next thing for me to look at more closely – user11797977 Jul 17 '19 at 15:35