使用带有自定义 UICollectionViewCell 的 FirebaseUI-IOS 时出错

Getting error when using FirebaseUI-IOS with custom UICollectionViewCell

我正在为我的 iOS 应用程序使用 Firebase-UI-IOS。当我 运行 应用程序时,出现以下错误:

fatal error: ”unexpectedly found nil while unwrapping an Optional value”.

这是我在 ViewDidLoad 中的代码:

    let ref = Firebase(url: "https://XXXXXX.firebaseio.com")
    var dataSource: FirebaseCollectionViewDataSource!
        let messageRef = ref
        .childByAppendingPath("brainstorms")
        .childByAppendingPath(invite.brainstormId)
        .childByAppendingPath("messages")

    self.dataSource = FirebaseCollectionViewDataSource(ref: messageRef, cellClass: MessageCollectionViewCell.self, reuseIdentifier: "messageCell", view: self.collectionView)

    self.dataSource.populateCellWithBlock { (cell: UICollectionViewCell, obj: NSObject) -> Void in
        // Populate cell as you see fit
        if let cell = cell as? MessageCollectionViewCell {
            println(cell)
            cell.messageText.text = "Hello world" <--- ERROR HERE
        }
    }

    self.collectionView.dataSource = self.dataSource

我试着打开 cell.messageText,但它总是 returns 没有。我的单元格 class MessageCollectionViewCell 已在我的 Storyboard 中注册。

我的 println(cell) 打印以下行:<xxxxx.MessageCollectionViewCell: 0x7fc26d8d9080; baseClass = UICollectionViewCell; frame = (17.5 155; 340 80); layer = <CALayer: 0x7fc26d8d92e0>>.

在我看来它应该可以正常工作。可以找到 populateCellWithBlock 的引用 here

有人有什么建议吗?

这里是 FirebaseUI 的创建者。

看起来 FirebaseUI 部分工作正常(我们的合同将 return 一个填充的单元格,它是 UICollectionViewCell 的子类和一个对象,它是 NSObject 的子类,它似乎在工作)。

问题似乎是您的自定义单元格中没有正确初始化 UILabel。你可以 post 你的 MessageCollectionViewCell.swift 文件吗?

它应该看起来像这样:

import Foundation
import UIKit

class MessageCollectionViewCell: UICollectionViewCell {

    @IBOutlet var mainLabel: UILabel?

    override init(frame: CGRect) {
        super.init(frame: frame)

        // Custom initialization code for label
        let size = self.contentView.frame.size
        let frame = CGRectMake(0.0, 0.0, size.width, size.height)
        self.mainLabel = UILabel(frame: frame)

        // Make sure you add the label as a subview
        self.contentView.addSubview(self.mainLabel!)
    }

  required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

}

收到 init(frame:) 电话后,它应该会适当填充。

我用以下 populateCellWithBlock 测试了这个:

dataSource.populateCellWithBlock { (cell: UICollectionViewCell, snap: NSObject) -> Void in
    let cell: MessageCollectionViewCell = cell as! MessageCollectionViewCell

    cell.mainLabel?.text = "Hello!"
} 

我正在努力添加 __kindof 支持以使签名看起来更像:

dataSource.populateCellWithBlock { (cell: MessageCollectionViewCell, snap: Message) -> Void in
    cell.mainLabel?.text = "Hello!"
} 

但是对 __kindof 的支持似乎参差不齐而且没有真正起作用(XCode 7 Beta 5 声称有支持,但我无法让 Cocoapods + Swift 接受它,即使它在 XCode 中构建并在 Objective-C) 中工作。

如果您对 FirebaseUI 有任何其他反馈,请告诉我,如果您发现任何其他错误,请随时将它们添加到 issue tracker:)