UIPickerView selection array Swift

∥☆過路亽.° 提交于 2021-01-28 05:49:58

问题


Currently, I got my code to work correctly, but the problem I'm having issue with the UIPickerView. Right now, If I select Cameron brand, it goes to var Cameron but if i select Shaffer brand, it still goes to var Cameron.

How can I rewrite the code so when I pick a different brand, it select a different array?

For example, if I select brand cameron, it select cameron, if i select brand Shaffer, it pick shaffer, etc.

class Picker: UIViewController, UIPickerViewDelegate, UITextFieldDelegate

{
    var activeTextField:UITextField?

    @IBOutlet var pickerView1: UIPickerView!
    @IBOutlet var pickerView2: UIPickerView!
    @IBOutlet var pickerView3: UIPickerView!

    @IBOutlet var textField1: UITextField!
    @IBOutlet var textField2: UITextField!
    @IBOutlet var textField3: UITextField!

    var brand = ["Cameron","Shaffer", "Hydril"]
    var cameron = ["D Annular Preventer", "UM Ram Preventer", "U Ram Preventer"]
    var shaffer = ["Spherical Annular Preventer", "LXT Ram Preventer", "NXT Ram Preventer"]
    var size = ["7 1/16","11","13 5/8"]
    var size2 = ["8 5/8","12","15 5/8"]

    override func viewDidLoad() {

        super.viewDidLoad()

        pickerView1 = UIPickerView()
        pickerView2 = UIPickerView()
        pickerView3 = UIPickerView()

        pickerView1.tag = 0
        pickerView2.tag = 1
        pickerView3.tag = 2 

    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int  {
        return 1
    }

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

        if pickerView.tag == 0 {
            return brand.count
        } else if pickerView.tag == 1 {
            return cameron.count
        } else if pickerView.tag == 2 {
            return size.count
        }
        return 1
    }

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

        if pickerView.tag == 0 {
            return brand[row]
        } else if pickerView.tag == 1 {
            return cameron[row]
        } else if pickerView.tag == 2 {
            return size[row]
        }
        return ""
    }

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

        if pickerView.tag == 0 {
            textField1.text = brand[row]
        } else if pickerView.tag == 1 {
            textField2.text = cameron[row]
        } else if pickerView.tag == 2 {
            textField3.text = size[row]
        } 
    }

回答1:


cameron has to be replaced with a dynamic lookup. You'll need a dictionary of brands, which you can then tie to the UI:

[As I was writing this up I found the variable names awful; please avoid 0, 1, 2, insanity of numbering your variables, 3, 4, bleah]

At class level:

var brandLookup = [ "cameron": [ "x", "y", "z"], "anotherBrand": ... ]

then your delegate functions:

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView.tag == 0 {
        return brand.count
    } else if pickerView.tag == 1 {
        let brand = textField1.text
        let modelsForBrand = brandLookup[brand]!
        return modelsForBrand.count
    }
    ...
}

similarly:

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)  {
   if pickerView.tag == 0 {
        textField1.text = brand[row]
        pickerView2.reloadAllComponents() // this picker has new data now
   } else if pickerView.tag == 1 {
       let brand = textField1.text
       let modelsForBrand = brandLookup[brand]!
       textField2.text = modelsForBrand[row]
   }
...
}

Finally:

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    // left as an exercise for you
}


来源:https://stackoverflow.com/questions/28932902/uipickerview-selection-array-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!