UIPickerView:未调用viewForRow

时间:2016-09-23 16:43:31

标签: swift uipickerview

我在设置UIPickerView时遇到问题。我已经把它全部设置为教科书,我想改变字体大小,但是:

  1. 通过attributionTitleForRow更改字体大小无效(尽管实际上更改了颜色)

  2. viewForRow 从不被调用。 完全

  3. 我正在使用Xcode 8& Swift 2.3并针对iOS 8+。以下是所有相关代码:

    Interface Builder(IB)连接:

    @IBOutlet weak var synapse: UIPickerView!
    

    链接到UIViewController的委托:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Préparation des gestionnaires de transition vers les différentes vues
        self.transitionManagerNav.sourceViewController = self
        self.transitionManagerNav.ordre = "travailleini2barrenav"
    
        self.transitionManagerDesk.sourceViewController = self
        self.transitionManagerDesk.ordre = "travailleini2desk"
    
        self.synapse.dataSource = self
        self.synapse.delegate = self
    
        dateEnCours = graphique.pointDeReference.dateComplete
    }
    

    代表实施:

    // UIPICKERVIEWDELEGATE
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return elementsDefileur.count
    }
    
    func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 17
    }
    
    func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return synapse.frame.width
    }
    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    }
    
    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        print("[viewForRow]") // THAT never happens (and yes, I am looking at the correct output window :o)
        let pickerLabel = UILabel()
        pickerLabel.textColor = UIColor.redColor()
        pickerLabel.text = elementsDefileur[row]
        pickerLabel.font = UIFont(name: "Arial-BoldMT", size: 5)
        pickerLabel.textAlignment = NSTextAlignment.Left
        return pickerLabel
    }
    

    我已经尝试了所有我能想到的,包括从代码中删除并重新创建UIPickerView,然后从(IB),将委托链接移动到viewWillAppear,titleForRow / attributionTitleForRow / viewForRow的每个组合,清理项目等

    如何解决此问题?

1 个答案:

答案 0 :(得分:3)

委托函数声明中的属性名称错误。它应该重新使用View'而不是重复使用view':

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

我相信Apple文档包含错误。我遇到了同样的问题,并通过修复上面的属性名称来修复它。