当我填充我的选择器视图时,我似乎无法显示所有三个选项。它只会给我最后选择的列结果。
import UIKit
class ViewThree: UIViewController, UIPickerViewDelegate {
@IBOutlet var sizePicker: UIPickerView!
var wheelContents:[[String]] = []
var one = ["1", "2"]
var two = ["A", "B", "C"]
var three = ["X", "Y"]
@IBOutlet var sparLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
wheelContents = [one, two, three]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return wheelContents.count
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return wheelContents[component].count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return wheelContents[component][row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var wheel = wheelContents[component][row]
sparLabel.text = "\(wheel)"
}
}
问题是我不知道在didSelectRow中放入什么{} 如果我将它们生成为单独的结果,那么每当我选择三行列的第3个选项时,它似乎都会崩溃。 如果我保持原样它只给出最后选择的列和行。 我希望它显示在一个标签中,例如" 1CY"。
由于
答案 0 :(得分:0)
var result = ""
for var i = 0; i < wheelContents.count; i++
{
let selectedRow = pickerView.selectedRowInComponent(i) //get selected row in component
let value = wheelContents[i][selectedRow] //get displayed value in component
result += value + " "
}
sparLabel.text = result