Mel
Mel

Reputation: 15

How to change my second pickerView's array based on the selection from the 1st pickerView

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

Upvotes: 0

Views: 51

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

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

Upvotes: 2

Related Questions