我有一个包含2个UIPickerviews的现有Viewcontroller。现在我想用另一个变量添加另一个UIPickerview。怎么做?
class ViewController: UIViewController,UIPickerViewDelegate {
@IBOutlet weak var pickerview1: UIPickerView!
@IBOutlet weak var pickerview2: UIPickerView!
@IBOutlet weak var pickerview3: UIPickerView!
var reps = ["1 x","2 x","3 x","4 x","5 x","6 x","7 x","8 x","9 x","10 x","11 x","12 x","13 x","14 x","15 x","16 x","17 x","18 x","19 x","20 x","21 x","22 x","23 x","24 x","25 x"]
var weight = [["0","1","2","3","4","5"],
["0","1","2","3","4","5","6","7","8","9"],
["0","1","2","3","4","5","6","7","8","9"],[","],
["0","25","5","75"],["kg","lbs"]]
var exercises = [["A","B","C"],["X","Y","Z"]]
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return ((pickerView == pickerview2) ? reps.count : weight.count)
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return ((pickerView == pickerview2) ? 1 : weight[component].count)
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return ((pickerView == pickerview2) ? reps[component] : weight[component][row])
}
第三个UIPickerview应该在Viewcontroller中显示变量练习。
答案 0 :(得分:0)
您可以使用switch语句
例如:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
switch pickerView {
case pickerview1:
return reps.count
case pickerview2:
return weights.count
case pickerview3:
return exercises.count
default:
assertionFailure("Unknown pickerView")
}
}
旁注:我已经匹配了你的变量名,但我认为你应该让你的pickerView变量名为camelCase,即pickerView1而不是pickerview1。