UIPickerView:未调用 viewForRow

UIPickerView: viewForRow not called

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

  1. 通过 attributedTitleForRow 更改字体大小没有效果(尽管更改颜色确实有效)

  2. viewForRow 从不 被调用。 完全.

我正在使用 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 / attributedTitleForRow / viewForRow 的每个组合,清理项目等

我该如何解决这个问题?

您的委托函数声明中的属性名称有误。应该是'reusingView',不是'reusing view':

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

我相信 Apple 文档包含错误。我有同样的问题,并通过如上所述修复属性名称来修复它。