Search code examples
swiftuicollectionviewuicollectionreusableview

What is the section number of a supplementary view?


I'm working with UICollectionViewController. At some point In need to make some configurations to the visible footers and for each one of them, I need to know its' current section number. Current because sections may be replaced and the section of a reusable view may change.

self.collectionView.visibleSupplementaryViews(ofKind: UICollectionView.elementKindSectionFooter)
    .filter { $0.reuseIdentifier == reuseIdentifierFooter}
    .forEach {
        // Here I need to know the section number of each view
}

Solution

  • Get the index path for the visible views: https://developer.apple.com/documentation/uikit/uicollectionview/1618034-indexpathsforvisiblesupplementar

    Then iterate through the indexPaths and query the view with https://developer.apple.com/documentation/uikit/uicollectionview/1618041-supplementaryview