如何使用 Swift 3 UIPickerView 引用多行?

Posted

技术标签:

【中文标题】如何使用 Swift 3 UIPickerView 引用多行?【英文标题】:How to reference multiple rows with Swift 3 UIPickerView? 【发布时间】:2016-11-13 09:27:50 【问题描述】:

如何在标签中显示“Lamborghini and White”?

我正在尝试弄清楚如何在每列中独立引用选定的行。

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource 

@IBOutlet var titlelbl: UILabel!
@IBOutlet var pickerView: UIPickerView!

var cars = [["BMW","Lamborghini","Range Rover", "Bentley",     "Maserati", "Rolls Royce"],["Blue","Green","White"]]

override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    pickerView.delegate = self
    pickerView.dataSource = self



func numberOfComponents(in pickerView: UIPickerView) -> Int 
    return 2


func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
    return cars[component].count


func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
    return cars[component][row]


func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
    titlelbl.text = "\(cars[0][row]) and \(cars[1][row])"

   

【问题讨论】:

【参考方案1】:

想通了,

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource 

    @IBOutlet var titlelbl: UILabel!
    @IBOutlet var pickerView: UIPickerView!

    var cars = [["BMW","Lamborghini","Range Rover", "Bentley", "Maserati", "Rolls Royce"],["Blue","Green","White"]]

    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        pickerView.delegate = self
        pickerView.dataSource = self

    


    func numberOfComponents(in pickerView: UIPickerView) -> Int 
        return 2
    

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
        return cars[component].count
    

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
        return cars[component][row]
    

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
        let selectedValue1 = pickerView.selectedRow(inComponent: 0)
        let selectedValue2 = pickerView.selectedRow(inComponent: 1)

       titlelbl.text = "\(cars[0][selectedValue1]) and \(cars[1][selectedValue2])"

    


【讨论】:

【参考方案2】:

[ func numberOfComponents(in pickerView: UIPickerView) -> Int 返回 2

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 

    if component == 0
       return bloodgroup.count
    
    else if component == 1 
       return districk.count
    
    return 1

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
    if component == 0
        return bloodgroup[row]
    
    else if component == 1 
        return districk[row]
    
   return ""


func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
    if component == 0
        print(bloodgroup[row])
    
    else if component == 1 
        print(districk[row])
    


]1

【讨论】:

以上是关于如何使用 Swift 3 UIPickerView 引用多行?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Swift 3 的 UIPickerView 中获取所选行的字符串值?

Swift 3:滚动视图中的 UIPickerView

Swift - 如何从 UIPickerView 生成数据的许多 UITextField 中读取数据 [关闭]

如何禁用 UIPickerView (Swift)?

进入uipickerview swift 3的国家列表

Swift 3 中的 UIPickerView 和子类数组