How do I reuse a view inside UIPickerView in this case?

感情迁移 提交于 2020-01-25 10:14:38

问题


It seems that a lot of people just return a UILabel, but in my case I'm adding a label to a view. The view is never not nil. This is a different use of UIPickerView, because I'm rotating its components 90 degrees and having it scroll sideways instead of up and down.

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

    if view == nil {

        let customWidth = 300
        let customHeight = 300

        let view = UIView(frame: CGRect(x:0, y:0, width:customWidth, height:customHeight))

        let label = UILabel(frame:CGRect(x:0, y:0, width:customWidth, height:customHeight))
        if let filter = filters.first(where: {$0.pickerViewRowIndex == row}) {
            var leading = ""
            if appPurchased == false && filter.requiresPurchase == true { leading = "🔒" }
            label.text = "\(leading)\(filter.nameStr)"
        }
        label.textColor = UIColor.black 
        label.font = UIFont(name:"HelveticaNeue-Bold", size: 18.0)
        label.textAlignment = .center

        view.addSubview(label)

        view.transform = CGAffineTransform(rotationAngle: 90 * (.pi/180))
        label.layer.masksToBounds = false

        return view

    }else{
        print("not nil") // console never prints this line
    }

    guard let reusedView = view else {
        assertionFailure("pickerView label view container never set")
        return view!
    }

    return reusedView
}

回答1:


The view is never not nil.

Correct. The docs are wrong. There is actually no such thing as reusing views in a picker view viewForRow. You always need to make a new view and return it.



来源:https://stackoverflow.com/questions/55380040/how-do-i-reuse-a-view-inside-uipickerview-in-this-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!