How to define sub-cell under collectionView in Swift?

Posted by Wei on June 21, 2017

Question

In a collectionView, there are many cells. I want to run different code under different cells, what should I do?

Answer

The answer below is using Swift 3.

In collectionView, multiple cells can be defined by indexPath.row, you run different code with this variable.

if (collectionView == self.collectionView1) {
  
  let cell : StandardCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: self.reuseIdentifierStandard, for: indexPath) as! StandardCollectionViewCell
  
  if indexPath.row == 0 {
    print("your code")
    cell.progressBar.progress = 0.2
  } else if indexPath.row == 1 {
    print("your code")
    cell.progressBar.progress = 0.4
  } else if indexPath.row == 2 {
    print("your code")
    cell.progressBar.progress = 0.6
  }
  
  return cell
}

This is the end of post