Try this:
1. Register two(or more) cells.
2.Configure cellforItem for each.
3. Configure sizeForItem for each.
First:
self.collectionView.register(SmallCell.self, forCellWithReuseIdentifier: "smallCell")
self.collectionView.register(BigCell.self, forCellWithReuseIdentifier: "bigCell")
And then:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if dataSource[indexPath.item].hasImage {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: “smallCell”, for: indexPath) as! SmallCell
let model = dataSource[indexPath.item]
cell.model = model
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: “bigCell”, for: indexPath) as! BigCell
let model = dataSource[indexPath.item]
cell.model = model
return cell
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if dataSource[indexPath.item].hasImage {
return CGSize(width: collectionView.frame.width, height: cellHeight+100)
} else {
return CGSize(width: collectionView.frame.width, height: cellHeight)
}
}