Home > front end >  Section Header For Particular Section Using UICollectionLayoutListConfiguration
Section Header For Particular Section Using UICollectionLayoutListConfiguration

Time:11-05

I have a UICollectionView with 2 sections using UICollectionLayoutListConfiguration. I want to have a Header for only one of those particular sections.

COLLECTION VIEW

lazy var collectionView: UICollectionView = {
    var list = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
    list.headerMode = .supplementary
    let layout = UICollectionViewCompositionalLayout.list(using: list)
    let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
    // REGISTER CELLS ETC...
    return view
}()

DATASOURCE

dataSource.supplementaryViewProvider = { [weak self] collectionView, kind, indexPath in
    guard let self = self else { return nil }
    
        
    if let section = Section(rawValue: indexPath.section) {
        switch section {
        case .topSection:
            // I DO NOT HAVE ANY HEADER FOR THIS SECTION
        case .bottomSection:
            switch kind {
            case UICollectionView.elementKindSectionHeader:
                // RETURN SECTION HEADER HERE
            case UICollectionView.elementKindSectionFooter:
                // I DO NOT HAVE ANY FOOTERS
            default:
                fatalError("SOMETHING BAD HAPPENED")
            }
        }
    }

Any help is greatly appreciated.

CodePudding user response:

Set header height as zero. for that particular section inside "referenceSizeForHeaderInSection"

CodePudding user response:

Solved it.

I needed need to return an NSCollectionLayoutSection for each section type in the UICollectionViewCompositionalLayout it cannot be done within the Data Source.

private func setUpLayout() -> UICollectionViewLayout {
        let layout = UICollectionViewCompositionalLayout { [unowned self] sectionIndex, layoutEnvironment in
            let section = Section(rawValue: sectionIndex)
            switch section {
            case .task:
                return self.setUpSection(layoutEnvironment: layoutEnvironment)
            case .project:
                return self.setUpHeaderSection(layoutEnvironment: layoutEnvironment)
            case .completed:
                return self.setUpHeaderSection(layoutEnvironment: layoutEnvironment)
            default:
                return self.setUpHeaderSection(layoutEnvironment: layoutEnvironment)
            }
        }
        return layout
    }
    
private func setUpSection(layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection {
        var listConfiguration = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
        listConfiguration.headerMode = .none
        return NSCollectionLayoutSection.list(using: listConfiguration, layoutEnvironment: layoutEnvironment)
    }
    
private func setUpHeaderSection(layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection {
        var listConfiguration = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
        listConfiguration.headerMode = .supplementary
        return NSCollectionLayoutSection.list(using: listConfiguration, layoutEnvironment: layoutEnvironment)
    }
  • Related