-1

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.

I use this code:

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
}

enter image description here

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
}

enter image description here

Dima
  • 111
  • 11

1 Answers1

0

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()
}
FortyTwo
  • 2,414
  • 3
  • 22
  • 33
  • I figured out the problem. What you said didn't help, but it helped me to understand what I have a problem. Thanks. – Dima Jan 13 '18 at 06:23
  • @Dima do you mind sharing what was your problem? I'm having a similar issue – budiDino Oct 16 '18 at 11:25