问题
- I have 2 uipickerviews in one uiviewcontroller:
dropdown_1.frame=CGRectMake(...); dropdown_2.frame=CGRectMake(...);
- I have 2 different array of values for these 2 dropdowns
> arraySizes5 = [sverysmall, ssmall, snormal, slarge, sverylarge]; > arraySizes3 = [ ssmall, snormal, slarge];
- I have function like :
.
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return arraySizes5.count; <<----- How to differentiate ??
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return arraySizes5[row]
}
How can I add the corresponding values to the 2 dropdowns in the function pickerView ? Or how can I set the component ID if that helps ?
回答1:
Here is the way you can do it:
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var dropdown_1: UIPickerView!
@IBOutlet weak var dropdown_2: UIPickerView!
var arraySizes5 = ["sverysmall", "ssmall", "snormal", "slarge", "sverylarge"]
var arraySizes3 = ["ssmall", "snormal", "slarge"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == dropdown_1 {
return arraySizes5.count
} else if pickerView == dropdown_2 {
return arraySizes3.count
}
return 0
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if pickerView == dropdown_1 {
return arraySizes5[row]
} else if pickerView == dropdown_2 {
return arraySizes3[row]
}
return ""
}
}
来源:https://stackoverflow.com/questions/31200987/how-to-distinguish-between-multiple-uipickerviews-on-one-page