根据第一个选择器中的哪一行更改第2和第3个pickerView

时间:2016-05-06 13:25:12

标签: swift uipickerview uipickerviewdatasource

我有3个这样的选择:第一个是generalPicker,第二个和第三个是相同的。在generalPicker中,我有一个数组(菜单):["长度","音量","质量"]以及每次我想选择时,例如来自generalPicker的弥撒,我想进入第二和第三个采摘者这个数组:[" Milligram"," Centigram"," Gram"," Kilogram"," Stone"," Pound"," Ounce"]。

或者,如果我选择了generalPicker,让我们说,长度,那么我想进入第二和第三选择器:["毫米","厘米&#34 ;," Meter"," Kilometer"," Foot"," Yard"," Mile"]。

任何人都可以告诉我如何才能真正做到这一点?我的代码示例(选择器配置):

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

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

    if pickerView.tag == 0 || pickerView.tag == 1 {
        return self.pickerLength.count
    } else if pickerView.tag == 2 {
        return self.pickerGeneral1.count
    }

    return 3
}

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

    if pickerView.tag == 0 || pickerView.tag == 1{
        return self.pickerLength[row]
    } else if pickerView.tag == 2 {
        return self.pickerGeneral1[row]
    }

    return ""
}

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

    if pickerView.tag == 0  {
        self.pickerTextField.text = self.pickerLength[row]
        self.view.endEditing(true)
    }else if pickerView.tag == 1 {
        self.pickedTextField2.text = self.pickerLength[row]
        self.view.endEditing(true)
    }else if pickerView.tag == 2 {
        self.pickerGeneral.text = self.pickerGeneral1[row]
        self.view.endEditing(true)
    }
}

阵列defs:

private var pickerMass = ["Milligram", "Centigram", "Gram", "Kilogram", "Stone", "Pound", "Ounce"]
private var pickerVolume = ["Milliliter", "Centiliter", "Liter", "Gallon", "Quart", "Pint", "Fluid ounce"]
private var pickerLength = ["Millimeter", "Centimeter", "Meter", "Kilometer", "Foot", "Yard", "Mile"]
private var pickerGeneral1 = ["Length", "Volume",  "Mass"]

2 个答案:

答案 0 :(得分:2)

以下是工作代码。这当然不是完成工作的好方法,但会提出解决问题的想法。

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet weak var textField_0: UITextField!
@IBOutlet weak var textField_1: UITextField!
@IBOutlet weak var textField_2: UITextField!

private var pickerGeneral1 = ["Length", "Volume", "Mass"]

// Maintaining the other arrays as single array of arrays for efficient loading 
private var subContentArray = [["Millimeter", "Centimeter", "Meter", "Kilometer", "Foot", "Yard", "Mile"],
    ["Milliliter", "Centiliter", "Liter", "Gallon", "Quart", "Pint", "Fluid ounce"],
    ["Milligram", "Centigram", "Gram", "Kilogram", "Stone", "Pound", "Ounce"]]

var picker_0 = UIPickerView()
var picker_1 = UIPickerView()
var picker_2 = UIPickerView()

// To keep track of user's current selection from the main content array
private var _currentSelection: Int = 0

// whenever current selection is modified, we need to reload other pickers as their content depends upon the current selection index only.
var currentSelection: Int {
    get {
        return _currentSelection
    }
    set {
        _currentSelection = newValue
        picker_1 .reloadAllComponents()
        picker_2 .reloadAllComponents()

        textField_0.text = pickerGeneral1[_currentSelection]
        textField_1.text = subContentArray[_currentSelection][0]
        textField_2.text = subContentArray[_currentSelection][0]
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    currentSelection = 0;

    picker_0.delegate = self
    picker_0.dataSource = self
    picker_0.tag = 0

    picker_1.delegate = self
    picker_1.dataSource = self
    picker_1.tag = 1

    picker_2.delegate = self
    picker_2.dataSource = self
    picker_2.tag = 2

    textField_0.inputView = picker_0
    textField_1.inputView = picker_1
    textField_2.inputView = picker_2
}

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

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView.tag == 0 {
        return pickerGeneral1.count
    } else {
        return subContentArray[currentSelection].count
    }
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView.tag == 0 {
        return pickerGeneral1[row]
    } else {
        return subContentArray[currentSelection][row]
    }
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if pickerView.tag == 0 {
        currentSelection = row

        textField_0.text = pickerGeneral1[row]
        textField_0.resignFirstResponder()
    } else if pickerView.tag == 1 {
        textField_1.text = subContentArray[currentSelection][row]
        textField_1.resignFirstResponder()
    } else if pickerView.tag == 2 {
        textField_2.text = subContentArray[currentSelection][row]
        textField_2.resignFirstResponder()
    }
}
}

我只使用一个UIPickerView而不是3.

答案 1 :(得分:0)

我认为您可以尝试使用字典而不是数组。类型为[String:String array]的字典意味着您的字典键将保存类别(例如Mass),并且该值将包含一系列类别项(例如毫克,厘米 - 都与Mass相关联。)

var dictionary: [String:[String]] = 
    [
        "length" : ["Millimeter", "Centimeter", "Meter", "Kilometer", "Foot", "Yard", "Mile"],
        "mass" : ["Milligram", "Centigram", "Gram", "Kilogram", "Stone", "Pound", "Ounce"]
    ]

if let item = dictionary["length"] {
     print(item)
} else {
     print("not found")
}