为什么UIPickerView显示空白列表

时间:2019-04-13 17:47:02

标签: ios swift uipickerview

我创建了两个UIPickerView并为每个来源声明。单击“标签”后,用户应会看到所选UIPickerView

的值
override func viewDidLoad() {
        super.viewDidLoad()
        print(monthBoolLabel)
        monthPickerView.dataSource = self
        monthPickerView.delegate = self
        monthPickerView.isHidden = true

        freeDayPickerView.dataSource = self
        freeDayPickerView.delegate = self
        freeDayPickerView.isHidden = true

        let tapMonth = UITapGestureRecognizer(target: self, action: #selector(tapOnMonth(gestureReconizer:)))
        monthLabel.addGestureRecognizer(tapMonth)
        monthLabel.isUserInteractionEnabled = true

        let tapFreeDay = UITapGestureRecognizer(target: self, action: #selector(tapOnFreeDay(gestureReconizer:)))
        freedayLabel.addGestureRecognizer(tapFreeDay)
        freedayLabel.isUserInteractionEnabled = true


        let tapView = UITapGestureRecognizer(target: self, action: #selector(backgroundViewTapped))
        view.addGestureRecognizer(tapView)
}

我声明了可以检测用户何时单击标签的功能:

@objc func tapOnMonth(gestureReconizer: UITapGestureRecognizer) {
        print("*")
        monthPickerView.isHidden = false
        monthBoolLabel = true
        freedayBoolLabel = false

    }

    @objc func tapOnFreeDay(gestureReconizer: UITapGestureRecognizer) {
        print("*")
        freeDayPickerView.isHidden = false
        freedayBoolLabel = true
        monthBoolLabel = false
    }

我还扩展了ViewController

extension HomePageViewController: UIPickerViewDelegate, UIPickerViewDataSource {

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if freedayBoolLabel == true {
            return freeDaySource.count
        }
        else if monthBoolLabel == true{
            return monthSource.count
        }
        else {
            return 0
        }
    }


    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if freedayBoolLabel == true {
            freedayLabel.text = freeDaySource[row]
        }
        else if monthBoolLabel == true{
            monthLabel.text = monthSource[row]
        }

    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if freedayBoolLabel == true {
            return freeDaySource[row]
        }
        else if monthBoolLabel == true{
            return monthSource[row]
        }
        else {
            return ""
        }
    }

}

无论用户单击UILabel,列表始终为空

1 个答案:

答案 0 :(得分:2)

点击标签后,您需要重新加载选择器视图。

AllBands

在另一个方面也进行了类似的更改。

仅供参考-您只需要一个选择器视图,而不是两个,因为无论如何一次只显示一个。