答案 0 :(得分:2)
1.设置来自故事板的dataSource或委托
2.从故事板中设置PickerOne的101标签。
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var pickerTwo: UIPickerView!
let pickerOneOptions = ["TV", "RADIO"]
let pickerTwoTVOptions = ["ABC", "SBS"]
let pickerTwoRadioOptions = ["TripleJ", "107.0", "CBS"]
var pickerOneSelectedRow:Int = 0
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 101 { //pickerOne
return pickerOneOptions.count
} else { //pickerTwo
if self.pickerOneSelectedRow == 0 {
return pickerTwoTVOptions.count
} else {
return pickerTwoRadioOptions.count
}
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 101 { //pickerOne
return pickerOneOptions[row]
} else { //pickerTwo
if self.pickerOneSelectedRow == 0 {
return pickerTwoTVOptions[row]
} else {
return pickerTwoRadioOptions[row]
}
}
return nil
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 101 {
self.pickerOneSelectedRow = row
pickerTwo.reloadAllComponents()
}
}
}
答案 1 :(得分:0)
请检查UIPickerViewDelegate和UIPickerViewDataSource: https://developer.apple.com/reference/uikit/uipickerviewdelegate https://developer.apple.com/reference/uikit/uipickerviewdatasource
这个想法是:
您可以根据所选行配置picker2的数据 picker1。
每当用户在picker1中选择一行时,重新加载picker2中的数据
以下代码:
class ViewController: UIViewController {
@IBOutlet weak var picker1: UIPickerView!
@IBOutlet weak var picker2: UIPickerView!
let picker1Options = ["TV", "RADIO"]
let picker2TVOptions = ["ABC", "SBS"]
let picker2RadioOptions = ["TripleJ", "107.0", "CBS"]
override func viewDidLoad() {
super.viewDidLoad()
self.picker1.dataSource = self
self.picker1.delegate = self
self.picker2.dataSource = self
self.picker2.delegate = self
// 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.
}
}
和
extension ViewController: UIPickerViewDataSource, UIPickerViewDelegate
{
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == self.picker1 {
return picker1Options.count
} else if picker1.selectedRow(inComponent: 0) == 0 {
return picker2TVOptions.count
}
else if picker1.selectedRow(inComponent: 0) == 1 {
return picker2RadioOptions.count
}
return 0
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == picker1 {
return picker1Options[row]
} else if picker1.selectedRow(inComponent: 0) == 0 {
return picker2TVOptions[row]
}
else if picker1.selectedRow(inComponent: 0) == 1 {
return picker2RadioOptions[row]
}
return nil
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == picker1 {
picker2.reloadAllComponents()
}
}
}