FirebaseUI: fatal error: unexpectedly found nil while unwrapping an Optional value Using Storyboards and UI Label
FirebaseUI: fatal error: unexpectedly found nil while unwrapping an Optional value Using Storyboards and UI Label
在使用 FirebaseUI 时,我试图为来自 Firebase 的数据实现自定义单元格。我想像这样在单元格中添加一些自定义标签:
这是我的集合视图控制器的样子:
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()
}
}
这是我的自定义单元格 class。真的很简单。
import UIKit
class DogCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var dogName: UILabel!
@IBOutlet weak var dogAge: UILabel!
@IBOutlet weak var dogToy: UILabel!
}
我已经在 github 上发布了代码:
https://github.com/thexande/firebaseCustomUICollectionViewCellDemo
以及描述该问题的视频:
我已经挑选了对这个问题的回答,似乎都涉及 XIB 而不是故事板。故事板不可能做到这一点吗?
谢谢大家!!!
好的。所以在尝试了一段时间后我想通了。
您需要通过self.dataSource = FirebaseCollectionViewDataSource(ref: self.firebaseRef, prototypeReuseIdentifier: reuseIdentifier, view: self.collectionView!)
设置数据源
具有 prototypeReuseIdentifier 的那个。否则,您不是在使用 DogCollectionViewCell
,而是创建了一个没有标签元素的 UICollectionViewCell 的新实例。这就是为什么你试图设置它的 .text
属性.
得到 nil
然后您可以通过代码设置年龄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
}
获取快照的值:
let nameSnap = snap.childSnapshot(forPath: "name")
dogCell.dogName.text = nameSnap.value! as? String
在使用 FirebaseUI 时,我试图为来自 Firebase 的数据实现自定义单元格。我想像这样在单元格中添加一些自定义标签:
这是我的集合视图控制器的样子:
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()
}
}
这是我的自定义单元格 class。真的很简单。
import UIKit
class DogCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var dogName: UILabel!
@IBOutlet weak var dogAge: UILabel!
@IBOutlet weak var dogToy: UILabel!
}
我已经在 github 上发布了代码:
https://github.com/thexande/firebaseCustomUICollectionViewCellDemo
以及描述该问题的视频:
我已经挑选了对这个问题的回答,似乎都涉及 XIB 而不是故事板。故事板不可能做到这一点吗?
谢谢大家!!!
好的。所以在尝试了一段时间后我想通了。
您需要通过self.dataSource = FirebaseCollectionViewDataSource(ref: self.firebaseRef, prototypeReuseIdentifier: reuseIdentifier, view: self.collectionView!)
具有 prototypeReuseIdentifier 的那个。否则,您不是在使用 DogCollectionViewCell
,而是创建了一个没有标签元素的 UICollectionViewCell 的新实例。这就是为什么你试图设置它的 .text
属性.
然后您可以通过代码设置年龄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
}
获取快照的值:
let nameSnap = snap.childSnapshot(forPath: "name")
dogCell.dogName.text = nameSnap.value! as? String