FirebaseUI和SDWebImage在CollectionViewControllerCell

时间:2019-02-23 21:24:45

标签: ios swift firebase uicollectionview sdwebimage

是否可以在for循环中为我的图像视图设置图像? 我目前正在从Firebase数据库获取所有图像下载URL,并将它们保存在[[String:Any]]类型的Dictionary中

在CollectionViewControllerFile的cellforItemAt方法内部,我当前正在调用这样的for循环:

   override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! MyImageCell

    for myurl in urlArray {
        print("Hier kommt myURL \(myurl)")
    let url = URL(string: myurl["url"] as! String)
        cell.imageView?.sd_setImage(with: url, completed: nil)
    }

    cell.myLabel?.text  = "test"

    return cell
}

但是这样,仅最后一张图像显示5次。

我在这里创建了一个自定义单元格并为其创建了一个类:

class MyImageCell: UICollectionViewCell {

@IBOutlet weak var imageView: UIImageView!

@IBOutlet weak var myLabel: UILabel!

}

我正在创建1个部分,每个部分5个项目。甚至有可能在for循环中使用SDWebImage还是我做错了什么?

1 个答案:

答案 0 :(得分:0)

您需要

let item = urlArray[indexPath.row]       
let url = URL(string: item["url"] as! String)
cell.imageView?.sd_setImage(with: url, completed: nil)

每次调用cellForItemAt都会返回一个单元格,因此,每当您最终将数组的最后一个URL设置为imageView时,也应创建一个像

这样的模型
struct Root {
  let url:URL
  // add more properties 
}

然后创建一个像这样的数组

var myArray = [Root]()