当UIPickerView具有多个组件时,如何仅显示一个组件?

时间:2019-07-05 12:29:09

标签: ios swift

我在UIPickerView中有两个Componenet。一个代表国家,另一个代表城市。我想在单击“国家/地区按钮”时仅显示一个组件,而在单击“城市”按钮时将显示另一个组件。

@IBOutlet weak var myPicker: UIPickerView!


    var pickerData = [["India", "Pakistan", "Bangladesh", "USA", "Afganistan", "Russia", "Nepal", "Bhutan"], ["Chandigarh", "Punjab", "Ludhiana", "Amritsar", "Shimal", "Una"]]

    override func viewDidLoad() {
        super.viewDidLoad()
        myPicker.isHidden = true

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return pickerData.count
    }

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

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

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
       // print(pickerData[row])
        showCountry.text = pickerData[component][row]

    }

    @IBAction func showCountry(_ sender: Any) {
        myPicker.isHidden = false
    }

现在,当我单击“国家/地区”时,它会显示两个组件,但我只想在pickerview中显示一个组件网。

2 个答案:

答案 0 :(得分:0)

如果要选择一个选择器,然后选择国家和城市

numberOfComponents 

应为 2

或者,如果您要选择一个选择器和国家/地区,然后再次选择城市

numberOfComponent

应为 1

但是要小心这种状态

//例如

@IBOutlet weak var myPicker: UIPickerView!


var pickerData = [["India", "Pakistan", "Bangladesh", "USA", "Afganistan", "Russia", "Nepal", "Bhutan"], ["Chandigarh", "Punjab", "Ludhiana", "Amritsar", "Shimal", "Una"]]

var isSelectingCountry = true

var selectedCountry: String!
var selectedCity: String!

override func viewDidLoad() {
    super.viewDidLoad()
    myPicker.isHidden = true

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData[isSelectingCountry ? 0 : 1].count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    let index = isSelectingCountry ? 0 : 1
    return pickerData[index][row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    // print(pickerData[row])
    //showCountry.text = pickerData[component][row]
    if isSelectingCountry {
        isSelectingCountry = false
        selectedCountry = pickerData[0][row]
    } else {
        selectedCity = pickerData[1][row]
    }
}

@IBAction func showCountry(_ sender: Any) {
    // if you want reset picker for start with country uncomment below line
    //isSelectingCountry = true
    myPicker.isHidden = false

}

,您可以使用 selectedCity selectedCountry

答案 1 :(得分:0)

您可以通过仅在要求的条件下处理它来完成

    import UIKit

class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {
    var pickerData = [["India", "Pakistan", "Bangladesh", "USA", "Afganistan", "Russia", "Nepal", "Bhutan"], ["Chandigarh", "Punjab", "Ludhiana", "Amritsar", "Shimal", "Una"]]
    @IBOutlet weak var myPicker: UIPickerView!
    var check : Int = 0
    override func viewDidLoad() {
        super.viewDidLoad()
         myPicker.isHidden = true
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        if check == 1 {
            return 1
        }
        else
        {
           return 1
        }

    }

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

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if check == 1 {
           return pickerData[0][row]
        }
        else
        {
             return pickerData[1][row]
        }

    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        // print(pickerData[row])
       // showCountry.text = pickerData[component][row]

    }

    @IBAction func showCountry(_ sender: Any) {
        check = 1
        myPicker.reloadAllComponents()
        myPicker.isHidden = false
    }

    @IBAction func showcity(_ sender: Any) {
        check = 2
        myPicker.reloadAllComponents()
        myPicker.isHidden = false
    }

}

希望这对您有帮助