UIPickerView没有显示任何值

时间:2014-12-31 13:49:50

标签: ios xcode swift

以下是我使用的代码:

// PickerView Functions

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

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

            return categoryArray.count // also tried with 5 or other numbers...

}

func pickerView(pickerView: UIPickerView, viewForRow row: Int,
    forComponent component: Int, reusingView view: UIView!) -> UIView {
        var lab : UILabel!
            if let label = view as? UILabel {
                lab = label
            } else {
                lab = UILabel()
            }
            lab.text = categoryArray[row] // also tried with text like "some test"
            lab.backgroundColor = UIColor.clearColor()
            lab.sizeToFit()
            return lab
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
       self.categoryLabel.text = categoryArray[row]
}

设置PickerViewDelegate并创建并连接所有商店...... 如果我运行此代码,它不会从categoryArray返回任何值,尽管这已经填充了值。我也尝试手动填充Picker,但这也没有用。可能是什么原因,任何想法?

2 个答案:

答案 0 :(得分:1)

感谢大家的评论,我发现了问题......我忘了在viewDidLoad中将委托设置为self,我只实现了委托类,确保无法工作;-)

答案 1 :(得分:1)

override func viewDidLoad() {
    super.viewDidLoad()

    picker.delegate = self
    picker.dataSource = self
    picker.tag = 1
}

希望它有效。