【问题标题】:Populate a collection view with firebase data使用 Firebase 数据填充集合视图
【发布时间】:2016-09-17 13:16:46
【问题描述】:

我正在尝试重新创建一个新的 firebase 项目,您可以在其中使用来自 firebase 实时数据库的数据填充表视图,其中包含指向 firebase 存储中图像的链接。

我可以使用 Firebase 数据填充作为表格视图的教程项目。但是对于我当前的项目,它是扩展内的集合视图。

我已将问题范围缩小到我的变量

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

当我尝试填充我的集合视图时会出现问题,因为我想要水平滚动我使用扩展。在扩展中,我发现我的值数组始终为 0,但在我的 loadPosts() 函数中,我的 >array 的计数与我在 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 上调用 reloadData()?
  • 哦,哇,我不是。一切都完美地填充了!谢谢! @MacBellingrath
  • 太棒了!我要发布一个答案。
  • 抱歉,您能否提供带有 self.collectionView.reloadData() 的更新代码以及它在哪里调用?

标签: ios swift firebase firebase-realtime-database firebase-storage


【解决方案1】:

您在完成块内正确地将项目插入到您的数组中,但是您错过了重新加载您的 collectionView 的调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    相关资源
    最近更新 更多