在添加viewForRow后,Swift UIPickerView不显示行

slmsl1lt  于 2022-11-21  发布在  Swift
关注(0)|答案(1)|浏览(96)

当我使用titleForRow时,我的UIPickerView工作正常。
但是,当我删除titleForRow并添加viewForRow以使其显示多行时,viewForRow为每行触发,但不显示行。
本守则........................

class CentralViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource

func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
    return 80.0
}

func open_mode_UIPickerView()
{
    oximeter_mode_UIPickerView = UIPickerView()
        oximeter_mode_UIPickerView.delegate = self
        oximeter_mode_UIPickerView.dataSource = self
        oximeter_mode_UIPickerView.backgroundColor = UIColor.white
        oximeter_mode_UIPickerView.setValue(UIColor.black, forKey: "textColor")
        oximeter_mode_UIPickerView.autoresizingMask = .flexibleWidth
        oximeter_mode_UIPickerView.contentMode = .center
        mode_UIPickerView_x =           0
        mode_UIPickerView_y =           10
        mode_UIPickerView_width =       Int( UIScreen.main.bounds.size.width / 3 )
        mode_UIPickerView_height =      300
        oximeter_mode_UIPickerView.frame = CGRect.init(    x: mode_UIPickerView_x, 
                                                            y: mode_UIPickerView_y, 
                                                            width: mode_UIPickerView_width, 
                                                            height: mode_UIPickerView_height)
        self.view.addSubview( oximeter_mode_UIPickerView )
        view.bringSubviewToFront( oximeter_mode_UIPickerView )
}


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

    if let view = view {
        label = view as! UILabel
    }
    else {
        label = UILabel(frame: CGRect(  x: mode_UIPickerView_x, 
                                         y: mode_UIPickerView_y, 
                                         width: mode_UIPickerView_width, 
                                         height: mode_UIPickerView_height ))
    }

    label.text = ""
        label.lineBreakMode = .byWordWrapping
        label.numberOfLines = 0
        // Use viewForRow, to support multiline
            if row == 0
            {
              label.text = "Oxi mode 0" 
                            + "\n Red    Red"
                            + "\n 620    660"
                print("titleForRow     label.text", label.text )       /////////  THIS PRINTS OK
            }                                                        
            if row == 1
            {
              label.text = "Oxi mode 1" 
                            + "\n Orange Red"
                            + "\n 590    730"
                print("titleForRow     label.text", label.text )       /////////  THIS PRINTS OK
            }                                                        
            if row == 2
            {
              label.text = "Oxi mode 2" 
                            + "\n Violet IR"
                            + "\n 420    830"
                print("titleForRow     label.text", label.text )       /////////  THIS PRINTS OK
            }                                                        
            if row == 3
            {
              label.text = "Oxi mode 3"
                            + "\n Green  IR"
                            + "\n 520    940"
                print("titleForRow     label.text", label.text )      /////////  THIS PRINTS OK
            }                                                        
        label.sizeToFit()
     return label
}

行标题结尾处的断点
〈用户界面标签:0x 104 e20 dd 0;帧=(0 10; 87.3333第61段); text = '氧气模式0红色红色6...';用户交互已启用=否;图层=〈_UI标签图层:0x281d830c0〉〉

uttx8gqw

uttx8gqw1#

问题......
字母标签〈〈〈〈〈〈〈〈〈〈〈错误。需要是类级别,而不是函数级别
即变量标签:UILabel!...在类别层级。

相关问题