sebastiansson
sebastiansson

Reputation: 307

Getting error when using FirebaseUI-IOS with custom UICollectionViewCell

I'm using Firebase-UI-IOS for my iOS app. When i run the app i get the following error:

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

Here is my code in 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

I've tried to unwrap cell.messageText, but it always returns nil. My cell class MessageCollectionViewCell is registered in my Storyboard.

My println(cell) prints the following line: <xxxxx.MessageCollectionViewCell: 0x7fc26d8d9080; baseClass = UICollectionViewCell; frame = (17.5 155; 340 80); layer = <CALayer: 0x7fc26d8d92e0>>.

This looks to me like it should be working. Reference to populateCellWithBlock can be found here.

Anyone got any suggestions?

Upvotes: 2

Views: 522

Answers (1)

Mike McDonald
Mike McDonald

Reputation: 15963

Creator of FirebaseUI here.

Looks like the FirebaseUI parts are working fine (our contract will return a populated cell that is a subclass of UICollectionViewCell and an object that is a subclass of NSObject, which appears to be working).

The issue seems to be that in your custom cell hasn't properly initialized the UILabel. Can you please post your MessageCollectionViewCell.swift file?

It should look something like this:

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)
    }

}

Once you've got an init(frame:) call, it should populate appropriately.

I tested this out with the following populateCellWithBlock:

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

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

I'm working on adding __kindof support to make the signature look more like:

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

But support for __kindof seems to be spotty and not really working (XCode 7 Beta 5 claims to have support, but I can't get Cocoapods + Swift to accept it, even though it builds in XCode and works in Objective-C).

Let me know if you've got any other feedback on FirebaseUI, and if you see any other bugs, feel free to add them to the issue tracker :)

Upvotes: 1

Related Questions