I have a PickerView and a Label like this:
我有一个PickerView和这样的标签:
I want to, when I change the value of each row in components, it will assign the value to the label above.
我想,当我更改组件中每一行的值时,它会将值分配给上面的标签。
For example: when I choose "Blue" in the first component and "Two" in the second component it will assign "Blue Two" to the Label.
例如:当我在第一个组件中选择“Blue”而在第二个组件中选择“Two”时,它会将“Blue Two”分配给Label。
I have tried like this, but it doesn't work well, it goes crash when I select the sixth value in the second component:
我尝试过这样,但是效果不好,当我在第二个组件中选择第六个值时会崩溃:
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]
}
}
2 个解决方案
#1
1
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var selectedValueFirstRow = pickerView.selectedRowInComponent(0)
var selectedValueSecondRow = pickerView.selectedRowInComponent(1)
colorLabel.text = colors[selectedValueFirstRow] + " " + numbers[selectedValueSecondRow]
}
Your code is crashing because its out of index for the first array
您的代码崩溃了,因为它超出了第一个数组的索引
#2
0
I add these lines in the top of ViewController
我在ViewController的顶部添加了这些行
var colorString = ""
var numberString = ""
and change the didSelectRow function like this:
并改变didSelectRow函数,如下所示:
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
colorString = array[component][row]
} else {
numberString = array[component][row]
}
resultLabel.text = colorString + " " + numberString
}
It works well now.
它现在运作良好。
#1
1
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var selectedValueFirstRow = pickerView.selectedRowInComponent(0)
var selectedValueSecondRow = pickerView.selectedRowInComponent(1)
colorLabel.text = colors[selectedValueFirstRow] + " " + numbers[selectedValueSecondRow]
}
Your code is crashing because its out of index for the first array
您的代码崩溃了,因为它超出了第一个数组的索引
#2
0
I add these lines in the top of ViewController
我在ViewController的顶部添加了这些行
var colorString = ""
var numberString = ""
and change the didSelectRow function like this:
并改变didSelectRow函数,如下所示:
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
colorString = array[component][row]
} else {
numberString = array[component][row]
}
resultLabel.text = colorString + " " + numberString
}
It works well now.
它现在运作良好。