Chewie The Chorkie
Chewie The Chorkie

Reputation: 5234

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

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
}

Upvotes: 0

Views: 351

Answers (1)

matt
matt

Reputation: 535169

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.

Upvotes: 2

Related Questions