Search code examples
swiftdynamiccomponentsuipickerview

Swift Dynamic UIPickerView


I feel like I’m close because the below code works for static UIPickerViews, but I’m trying to dynamically setup my pickerview based on the 1st selection and I can’t wrap my mind around this despite reviewing tutorials and searching. For example, if you select “Brown” in myPickerData only “Undergraduate” should display in myPickerData2 or if you select “Dartmouth”, you should only get “Undergraduate” and “MBA” and last if you select “Columbia” or “Harvard”, you get “Undergraduate”, “JD”, “MBA”.

var myPickerData = ["Select Your Dream College", "Brown", "Columbia", "Dartmouth",  "Harvard" ]

var myPickerData2 = ["Undergraduate", "JD", "MBA"]

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

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

    if component == 0 {
        return myPickerData.count
    }

    return myPickerData2.count
}

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

    if component == 0 {
        return myPickerData[row] as String
    }
    return myPickerData2[row] as String
}

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

    uniSelected = myPickerData[pickerView.selectedRow(inComponent: 0)]

    collegeSelected = myPickerData2[pickerView.selectedRow(inComponent: 1)]
}

Solution

  • You can try

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    
         if component == 0 {
    
             let str = myPickerData[row]
    
             if str == "Brown" {
    
                myPickerData2 = ["Undergraduate"]
             }
             else if str == "Dartmouth" {
    
                myPickerData2 = ["Undergraduate", "MBA"]
             }
             else {
    
                myPickerData2 = ["Undergraduate", "JD", "MBA"]
    
             }
    
             pickerView.reloadComponent(1)
    
          }
          else {
    
             uniSelected = myPickerData[pickerView.selectedRow(inComponent: 0)]
    
             collegeSelected = myPickerData2[pickerView.selectedRow(inComponent: 1)]
          }
    
    }
    

    //

    This may contain duplicates but you can try it

    let mainDic:[String:[String]] = ["Select Your Dream College":[],
                     "Brown":["Undergraduate"],
                     "Dartmouth":["Undergraduate", "MBA"],
                     "Columbia":  ["Undergraduate", "JD", "MBA"],
                     "Harvard",["Undergraduate", "JD", "MBA"]]
    

    Then

    myPickerData2 = mainDic[myPickerData[row]]!
    
    pickerView.reloadComponent(1)
    

    And use this in component 0

    myPickerData = Array(mainDic.keys)
    

    Make your dataSource is mainDic and deal with keys as myPickerData and currently selected key's value as myPickerData2

    Edit : In viewDidLoad

    myPickerData = Array(mainDic.keys)
    myPickerData2 = mainDic["Select Your Dream College"]! // it's default or simply []