我在我的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}}。
有人有任何建议吗?
答案 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:)