Пример из моего проекта. (
Код не привожу текстом, ужасный редактор.)
В делегате:
internal override func collectionView(
_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath
) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: CategoriesCollectionViewCell.reuseIdentifier,
for: indexPath
) as! CategoriesCollectionViewCell
let randomIndex = indexPath.item % CategoriesPlaceholder.images.count
cell.configure(
withImage: UIImage(named: CategoriesPlaceholder.images[randomIndex])!,
andTitle: CategoriesPlaceholder.titles[randomIndex]
)
return cell
}
Внутри ячейки:
internal final class CategoriesCollectionViewCell: UICollectionViewCell {
// =====================================
// MARK: - Configure
// =====================================
internal func configure(withImage image: UIImage?, andTitle title: String) {
illustrationCircle.image = image
self.title.text = title
}