简体   繁体   English

Swift中的UIPickerView子类:viewForRow中的可选值错误

[英]UIPickerView subclass in Swift : error optional value in viewForRow

I would like to subclass UIPickerView in order to create a custom DatePicker. 我想对UIPickerView进行子类化,以创建一个自定义的DatePicker。 I tried this, but there is an error in Swift saying : 我试过了,但是Swift说一个错误:

unexpectedly found nil while unwrapping an Optional value 展开Optional值时意外发现nil

on this line : (view.viewWithTag(1) as UILabel).text = array[row] 在这一行: (view.viewWithTag(1) as UILabel).text = array[row]

Here is the code : 这是代码:

class MyPickerView : UIPickerView, UIPickerViewDataSource, UIPickerViewDelegate{

    let array = ["one", "two", "three"]
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame:frame)
        self.delegate = self
        self.dataSource = self
    }

    //components / row
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return array.count
    }

    //view for row
    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
        if view == nil {
            var view = UIView(frame: CGRectMake(0,0, 150,50))
            let label = UILabel(frame:CGRectMake(0,0, 80, 40))
            label.tag = 1
            view.addSubview(label)
        }
        (view.viewWithTag(1) as UILabel).text = array[row]
        return view
    }
    //did select row
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        println("has selected row \(row)")
    }

Thanks 谢谢

There is a known bug in UIPickerView (started in iOS 7 I think), that the views are never reused. UIPickerView中存在一个已知的错误(我认为它始于iOS 7),该视图永远不会被重用。 To fix your problem, get rid of the if clause (then there's no need for the tag), 要解决您的问题,请删除if子句(这样就不需要标记了),

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
    var view = UIView(frame: CGRectMake(0,0, 150,50))
    let label = UILabel(frame:CGRectMake(0,0, 80, 40))
    view.addSubview(label)
    label.text = array[row]
    return view
}

The reason you get the error, is because you have two different objects called "view". 出现错误的原因是因为您有两个不同的对象,称为“视图”。 The one you create inside the if clause is not visible outside that clause, so the "view" you're calling viewWithTag on is the view that's passed in to the method, and that one is always nil. 您在if子句中创建的那个在该子句之外不可见,因此您正在调用viewWithTag的“视图”是传递给该方法的视图,并且该视图始终为nil。

First you check if view is nil, but after the closure you try to access it whether it's nil or not. 首先,您检查view是否为nil,但是在关闭之后,您尝试访问它是否为nil。 If I understood your intention correctly, you should replace 如果我正确理解了您的意图,则应该更换

var view = UIView(frame: CGRectMake(0,0, 150,50))

with

view = UIView(frame: CGRectMake(0,0, 150,50))

because at the moment you are creating a new variable called view within the scope of if statement. 因为目前您正在if语句范围内创建一个名为view的新变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM