【问题标题】:Firebase storage data showing twiceFirebase 存储数据显示两次
【发布时间】:2017-07-17 00:10:49
【问题描述】:

尝试使用此方法在集合视图中加载数据

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if urls.count == 0 {
        return UICollectionViewCell()
    }
    let url = urls[indexPath.item]
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,
                                                  for: indexPath) as! ImageCollectionViewCell
    storageRef.reference(withPath: url).getData(maxSize: 15 * 1024 * 1024, completion:  { data, error in
            if let data = data {                   
                cell.imageView.image = UIImage(data: data)
            }
        })
    return cell
}

问题是 - 起初我可以看到两个没有数据的单元格,然后调用完成,我得到一个数据,但两个单元格中的第一个图像。 我怎样才能做对?我怎样才能同时获取元数据?

截图:

更新:

我写了一个数组

    var datas = ["first", "second", "third", "fourth"]

将单元格代码更改为

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,
                                                  for: indexPath) as! ImageCollectionViewCell
    cell.nameLabel.text = datas[indexPath.item]
    return cell
}

得到了这个:

仍然看不出有什么问题。我的重写方法:

    func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 2
}

func collectionView(_ collectionView: UICollectionView,
                    numberOfItemsInSection section: Int) -> Int {
    return datas.count / 2
}

已解决。应该返回 numberOfSections = 1

【问题讨论】:

  • 你也可以添加截图吗?
  • @TalhaQ 我会,但我不认为它会有用
  • @TalhaQ,按要求
  • 我相信你需要在collectionview范围之外使用这个storageRef.reference(withPath: url).getData。将数据保存在数组中并在此处填充
  • 在函数中从 cellForItemAt 外部的存储中获取图像并在数组中接收所有图像,然后使用相应的数组在单元格中显示图像将解决您的问题

标签: ios swift firebase firebase-storage


【解决方案1】:

我认为问题在于您正在 cellForItemAt indexPath: 函数中下载信息,您应该在另一个函数中下载图像

//Variable to store the UIImages
    var images = [UIImage]()

//function to download the images, run it in the viewdidload like self.getImages()
    func getImages(){

        for url in self.urls{

            storageRef.reference(withPath: url).getData(maxSize: 15 * 1024 * 1024, completion:  { data, error in
                if let data = data {
                    self.images.append(UIImage(data: data))
                }
            })
        }

        self.YourCollectionView.reloadData()

    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if urls.count == 0 {
            return UICollectionViewCell()
        }

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,
                                                      for: indexPath) as! ImageCollectionViewCell
        cell.imageView.image = self.images[indexPath.row]

        return cell
    }

【讨论】:

    【解决方案2】:

    应该在 numberOfSection 函数中返回 1

    func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 2
    

    }

    【讨论】:

      猜你喜欢
      • 2017-05-15
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      相关资源
      最近更新 更多