1
我有一個PickerView和這樣的標籤:如何從pickView得到確切的數值與多個組件
我想,當我改變每行的組成部分的價值,它會將值分配給上面的標籤。
例如:當我在第一個組件中選擇「Blue」並在第二個組件中選擇「Two」時,它將爲標籤分配「Blue Two」。
我已經試過這樣的,但它不能很好地工作,它會崩潰,當我選擇在第二組分中的第六個值:
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet var colorLabel: UILabel!
let colors = ["Red", "Green", "Blue", "Black", "White"]
let numbers = ["One", "Two", "Three", "Four" ,"Five", "Six", "Seven"]
var array = [[String]]()
override func viewDidLoad() {
super.viewDidLoad()
array = [colors, numbers]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return array.count
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return array[component].count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return array[component][row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
colorLabel.text = component == 0 ? array[component][row] + " " + array[component][row] : array[component][row] + " " + array[component][row]
}
}