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

问题描述 投票:0回答:2

如何在标签上显示“兰博基尼和白色”?

我试图找出如何在每列中独立引用所选行。

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])"
}
   }
swift3 uipickerview
2个回答
0
投票

弄清楚了,

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])"

    }

}

0
投票

[func numberOfComponents(在pickerView:UIPickerView中) - > Int {return 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

© www.soinside.com 2019 - 2024. All rights reserved.