使用 Firebase 数据填充集合视图
Populate a collection view with firebase data
我正在尝试重新创建一个新的 firebase 项目,您可以在其中使用来自 firebase 实时数据库的数据填充 table 视图,其中包含指向 firebase 存储中图像的链接。
我可以使用 firebase 数据填充 table 视图的教程项目。但是对于我当前的项目,它是扩展中的集合视图。
我已将问题缩小到我的变量
var ref: FIRDatabaseReference!
var messages: [FIRDataSnapshot]! = []
var msglength: NSNumber = 10
private var _refHandle: FIRDatabaseHandle!
特别是
var messages: [FIRDataSnapshot]! = []
我认为这是我从 firebase 获得的数据数组
然后我调用一个函数,该函数应该在我的 viewdidload() 中填充该数组
func loadPosts(){
self.messages.removeAll()
// Listen for new messages in the Firebase database
_refHandle = self.ref.child("messages").observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in
//print("1")
self.messages.append(snapshot)
//print(self.messages.count)
})
}
The issue happens when I try to populate my collections view since I want horizontal scrolling I use an extension. In the extension I find that my array of values is always 0, but in my loadPosts() function the count of my >array is the same value as the amount of posts I have in firebase.
extension HomeViewController : UICollectionViewDataSource
{
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return messages.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
print(messages.count)
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(StoryBoard.CellIdentifier, forIndexPath: indexPath) as! InterestCollectionViewCell
// Unpack message from Firebase DataSnapshot
let messageSnapshot: FIRDataSnapshot! = self.messages[indexPath.row]
let message = messageSnapshot.value as! Dictionary<String, String>
let name = message[Constants.MessageFields.name] as String!
if let imageUrl = message[Constants.MessageFields.imageUrl] {
if imageUrl.hasPrefix("gs://") {
FIRStorage.storage().referenceForURL(imageUrl).dataWithMaxSize(INT64_MAX){ (data, error) in
if let error = error {
print("Error downloading: \(error)")
return
}
cell.featuredImageView?.image = UIImage.init(data: data!)
}
} else if let url = NSURL(string:imageUrl), data = NSData(contentsOfURL: url) {
cell.featuredImageView?.image = UIImage.init(data: data)
}
cell.interestTitleLabel?.text = "sent by: \(name)"
}
return cell
}
}
我不应该使用 FIRDataSnapshot 吗?如果是这样,使用哪个是正确的?或者我应该以不使用扩展的另一种形式处理该项目?
您在完成块中正确地将项目插入到数组中,但是您错过了重新加载 collectionView 的调用。
我正在尝试重新创建一个新的 firebase 项目,您可以在其中使用来自 firebase 实时数据库的数据填充 table 视图,其中包含指向 firebase 存储中图像的链接。
我可以使用 firebase 数据填充 table 视图的教程项目。但是对于我当前的项目,它是扩展中的集合视图。
我已将问题缩小到我的变量
var ref: FIRDatabaseReference!
var messages: [FIRDataSnapshot]! = []
var msglength: NSNumber = 10
private var _refHandle: FIRDatabaseHandle!
特别是
var messages: [FIRDataSnapshot]! = []
我认为这是我从 firebase 获得的数据数组
然后我调用一个函数,该函数应该在我的 viewdidload() 中填充该数组
func loadPosts(){
self.messages.removeAll()
// Listen for new messages in the Firebase database
_refHandle = self.ref.child("messages").observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in
//print("1")
self.messages.append(snapshot)
//print(self.messages.count)
})
}
The issue happens when I try to populate my collections view since I want horizontal scrolling I use an extension. In the extension I find that my array of values is always 0, but in my loadPosts() function the count of my >array is the same value as the amount of posts I have in firebase.
extension HomeViewController : UICollectionViewDataSource
{
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return messages.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
print(messages.count)
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(StoryBoard.CellIdentifier, forIndexPath: indexPath) as! InterestCollectionViewCell
// Unpack message from Firebase DataSnapshot
let messageSnapshot: FIRDataSnapshot! = self.messages[indexPath.row]
let message = messageSnapshot.value as! Dictionary<String, String>
let name = message[Constants.MessageFields.name] as String!
if let imageUrl = message[Constants.MessageFields.imageUrl] {
if imageUrl.hasPrefix("gs://") {
FIRStorage.storage().referenceForURL(imageUrl).dataWithMaxSize(INT64_MAX){ (data, error) in
if let error = error {
print("Error downloading: \(error)")
return
}
cell.featuredImageView?.image = UIImage.init(data: data!)
}
} else if let url = NSURL(string:imageUrl), data = NSData(contentsOfURL: url) {
cell.featuredImageView?.image = UIImage.init(data: data)
}
cell.interestTitleLabel?.text = "sent by: \(name)"
}
return cell
}
}
我不应该使用 FIRDataSnapshot 吗?如果是这样,使用哪个是正确的?或者我应该以不使用扩展的另一种形式处理该项目?
您在完成块中正确地将项目插入到数组中,但是您错过了重新加载 collectionView 的调用。