No Storyboard!
I have a collectionView with a diffeable datasource. Only one section. I want to display a header, so I registered a custom cell class:
collectionView.register(SectionHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: SectionHeader.reuseidentifier)
I create a flow Layout as such:
private func createFlexibleFlowLayout(in view: UIView) -> UICollectionViewFlowLayout {
let width = view.bounds.width
let padding: CGFloat = 12
let minimumItemSpacing: CGFloat = 10
let availableWidth = width - (padding * 2) - (minimumItemSpacing * 2)
let itemWidth = traitCollection.horizontalSizeClass == .compact ? availableWidth / 2 : availableWidth / 3
let flowLayout = UICollectionViewFlowLayout()
flowLayout.sectionInset = UIEdgeInsets(top: padding, left: padding, bottom: padding, right: padding)
flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth + 40)
flowLayout.sectionHeadersPinToVisibleBounds = true
return flowLayout
}
The datasource looks as follows:
func createDataSource() {
dataSource = UICollectionViewDiffableDataSource<Section, Project>(collectionView: collectionView) { (collectionView, indexPath, project) in
return self.configure(NormalProjectCell.self, with: project, for: indexPath)
}
dataSource?.supplementaryViewProvider = { (collectionView, kind, indexPath) in
guard let sectionHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: SectionHeader.reuseidentifier, for: indexPath) as? SectionHeader else {
fatalError("Could not dequeue sectionHeader: \(SectionHeader.reuseidentifier)")
}
sectionHeader.title.text = "Tap and hold to edit an existing Project!"
sectionHeader.subtitle.text = "tap to view Project notes"
return sectionHeader
}
}
But how and where do I add the header? The 'old way' doesn't work:
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {(...)}
I'm stumped!