在两个组件pickerView中查找两个变量

时间:2017-07-11 06:47:54

标签: ios swift multidimensional-array uipickerview

我找不到任何解释如何从pickerView的两个组件获取信息的地方。现在它所做的就是将最后移动的组件中的String放入测试标签中。我真的不需要他们在网点,但只是访问它们。我也想使用我注释掉的数组格式。

@IBOutlet weak var itemPickerView: UIPickerView!

var pickerViewArray = [["Items", "Key", "Rock"], ["Action", "Turn", "Hit"]]

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return pickerViewArray.count
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerViewArray[component].count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerViewArray[component][row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    itemSelected = pickerViewArray[component][row]
    pickerOutletTest.text = itemSelected
}

@IBOutlet weak var pickerOutletTest: UILabel!

2 个答案:

答案 0 :(得分:0)

您可以使用

获取所有组件中所选项目的数组
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    let selectedItems = (0..<pickerViewArray.count).map{  pickerViewArray[$0][pickerView.selectedRow(inComponent: $0)]}
    print(selectedItems)
}

组件索引对其选定值的map ping是

的缩写形式
var selectedItems = [String]()
for component in 0..<pickerViewArray.count {
    let selectedRow = pickerView.selectedRow(inComponent: component)
    selectedItems.append(pickerViewArray[component][selectedRow])
}

答案 1 :(得分:0)

将didSelectRow方法改为低于一。

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
     pickerOutletTest.text = pickerViewArray[0][pickerView.selectedRow(inComponent: 0)] + " " + pickerViewArray[1][pickerView.selectedRow(inComponent: 1)]
}