UIPickerView选择数组Swift

时间:2015-03-08 22:55:58

标签: xcode swift uipickerview

目前,我的代码工作正常,但问题是我遇到了UIPickerView的问题。现在,如果我选择Cameron品牌,那么它将归属于Cameron品牌,但如果我选择Shaffer品牌,它仍然适用于var Cameron。

如何重写代码,以便在选择其他品牌时,选择不同的数组?

例如,如果我选择品牌卡梅隆,它选择卡梅隆,如果我选择品牌谢弗,它选择谢弗等。

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 个答案:

答案 0 :(得分:2)

cameron必须替换为动态查找。您需要一个品牌词典,然后您可以将其与UI联系起来:

[当我写这篇文章时,我发现变量名称很糟糕;请避免0,1,2,编号变量的精神错误,3,4,bleah]

在班级:

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

然后你的代表职能:

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
    }
    ...
}

类似地:

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]
   }
...
}

最后:

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