How do I reuse a view inside UIPickerView in this

2019-08-19 04:10发布

问题:

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 = "