我找不到任何解释如何从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!
答案 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)]
}