图像未显示在集合视图单元格中 [英] Images not displayed in collection view cell

查看:27
本文介绍了图像未显示在集合视图单元格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合视图单元格,里面有一个 imageView.在它上面,我使用 sd_setImage 从 firestore 加载后显示图像数组.在控制台中,我看到我的应用程序下载的所有图像.

I have a collection view cell and inside there's an imageView. On it I display array of images using sd_setImage once loaded from firestore. In console I see all images which my app downloaded.

我使用此代码:

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "newDetailCollectionCell", for: indexPath) as! NewDetailCollectionViewCell
    cell.imageView.sd_setImage(with: URL(string: images[indexPath.item]))

    return cell
}

当我使用此代码时,我看不到我的图像.但是如果我使用下面的代码一切正常.

When I use this code I can't see my images. But if I use the code below everything works fine.

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "newDetailCollectionCell", for: indexPath) as! NewDetailCollectionViewCell
    cell.imageView.sd_setImage(with: URL(string: images[indexPath.item]))

    return cell
}

推荐答案

问题是你应该调用 collectionView.reloadData() 而不是 tableView.reloadData()> 在您的 Dispatch.main.async {} 因为在这种情况下 collectionView 需要更新数据源.我希望这会有所帮助.

The problem is that you should be calling collectionView.reloadData() instead of tableView.reloadData() in your Dispatch.main.async {} Because in this situation the collectionView needs to update the dataSource. I Hope this helps.

DispatchQueue.main.async {
  self.collectionView?.reloadData()
}

这篇关于图像未显示在集合视图单元格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆