title: How to show activity indicator in UICollectionView cell?
body: <p>I'm trying to show activity indicator for every cell until the image for UICollectionView cell show up, but it only shown in the last cell.</p>
  
  <p><a href="https://i.stack.imgur.com/crcra.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/crcra.png" alt="view"></a></p>
  
  <p>My code:</p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath)
  
      cell.backgroundColor = .blue
      let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 160, height: 200))
      let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 160, height: 200))
  
      cell.contentView.addSubview(activityIndicator)
      activityIndicator.startAnimating()
  
      containerView.backgroundColor = .magenta
  
      containerView.addSubview(imageView)
  
      guard imageArray.count == tempListLength &amp;&amp; tempListLength &gt; 0 else {
          DispatchQueue.main.async {
              self.activityIndicator.center = cell.contentView.center
          }
          return cell
      }
  
      DispatchQueue.main.async {
          self.activityIndicator.stopAnimating()
      }
      imageView.image = imageArray[indexPath.row]
  
      cell.contentView.addSubview(imageView)
  
      return cell
  }
  </code></pre>