将FirebaseUI-IOS与自定义UICollectionViewCell一起使用时出错

时间:2015-08-18 15:11:57

标签: ios iphone swift uicollectionview firebase

我在我的iOS应用中使用Firebase-UI-IOS。当我运行应用程序时,我收到以下错误:

  

致命错误:“在打开Optional时意外发现nil   值”。

以下是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,但它总是返回nil。我的单元格类MessageCollectionViewCell已在我的Storyboard中注册。

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

这让我觉得它应该有效。可以找到对populateCellWithBlock的引用{。{3}}。

有人有任何建议吗?

1 个答案:

答案 0 :(得分:1)

FirebaseUI的创建者。

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

问题似乎是您的自定义单元格未正确初始化UILabel。您能否发布您的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:)