sguruapp@gmail.com
facebook vk instagram Телеграм youtube

Добавить надпись для секций в collectionView (Add a section title in UICollectionView) - Swift 5


// 1. Создать класс для работы с секцией
class CategoriesCollectionReusableView: UICollectionReusableView {
@IBOutlet weak var headerLabel: UILabel!
}

// 2. Привязать класс CategoriesCollectionReusableView к CollectionReusableView

// 3. Нужно указать identifier для CollectionReusableView

// 4. Вызвать метод func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView и написать код


override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if let sHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "CartHeaderCollectionReusableView", for: indexPath) as? CategoriesCollectionReusableView{

sHeader.headerLabel.text = "Section \(indexPath.section)"
return sHeader
}

return UICollectionReusableView()
}

Возврат к списку

обучись профессии будущего

Хочешь стать успешным разработчиком
приложений? Тогда добро пожаловать к нам.

обучись профессии будущего