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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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