While using FirebaseUI, I am attempting to implement a custom cell for my data from firebase. I would like to have a few custom labels in the cell like so:
Here is what my collection view controller looks like:
import UIKit
import Firebase
import FirebaseDatabaseUI
private let reuseIdentifier = "Cell"
class ShowDogsCollectionViewController: UICollectionViewController {
let firebaseRef = FIRDatabase.database().reference().child("dogs")
var dataSource: FirebaseCollectionViewDataSource!
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = FirebaseCollectionViewDataSource(ref: self.firebaseRef, cellClass: DogCollectionViewCell.self, cellReuseIdentifier: reuseIdentifier, view: self.collectionView!)
self.dataSource.populateCell { (cell: UICollectionViewCell, obj: NSObject) -> Void in
let snap = obj as! FIRDataSnapshot
let dogCell = cell as! DogCollectionViewCell
dogCell.backgroundColor = UIColor.green
print(snap.childSnapshot(forPath: "name"))
// The line below should set the label text for one of the labels on our custom UICollectionCell Class, however it unwraps to nil.
// fatal error: unexpectedly found nil while unwrapping an Optional value
// dogCell.dogAge.text = "woot"
}
self.collectionView?.dataSource = self.dataSource
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
And this is my custom cell class. Real Simple.
import UIKit
class DogCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var dogName: UILabel!
@IBOutlet weak var dogAge: UILabel!
@IBOutlet weak var dogToy: UILabel!
}
I have posted the code on github here:
https://github.com/thexande/firebaseCustomUICollectionViewCellDemo
As well as a video describing the issue here:
I have picked through the responses to this question, and all seem to involve XIB and not story board. Is this not possible with the story board?
Thanks All!!!
Okay. So while trying for a while I figured it out.
You need to set the dataSource by self.dataSource = FirebaseCollectionViewDataSource(ref: self.firebaseRef, prototypeReuseIdentifier: reuseIdentifier, view: self.collectionView!)
The one with the prototypeReuseIdentifier. Otherwise you're not using your DogCollectionViewCell
, but creating a new instance of UICollectionViewCell that does not have the label elements. That is why you're getting nil by trying to set its .text
property.
Then you can set the age by your code dogCell.dogAge.text = "woot"
.
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = FirebaseCollectionViewDataSource(ref: self.firebaseRef, prototypeReuseIdentifier: reuseIdentifier, view: self.collectionView!)
self.dataSource.populateCell { (cell: UICollectionViewCell, obj: NSObject) -> Void in
let snap = obj as! FIRDataSnapshot
let dogCell = cell as! DogCollectionViewCell
dogCell.backgroundColor = UIColor.green
dogCell.dogAge.text = "woot"
}
self.collectionView?.dataSource = self.dataSource
}
To get the values of the snap:
let nameSnap = snap.childSnapshot(forPath: "name")
dogCell.dogName.text = nameSnap.value! as? String