Thirdly, register cells and supplementary views for collection view in func registerCell(collectionView: UICollectionView) and func registerSupplementaryView(collectionView: UICollectionView).
func registerCell(collectionView: UICollectionView) {
// register cells here
}
func registerSupplementaryView(collectionView: UICollectionView) {
// register supplementary views here
}
Make sure that you implement func supplementaryView(_ collectionView: UICollectionView, kind: String, indexPath: IndexPath) -> UICollectionReusableView?.
You may have to deque UICollectionReusableView from a collection view.
These cells and supplementary views are configured following methods.
CompositionalLayoutViewController
Example
To run the example project, clone the repo, and run
pod install
from the Example directory first.Requirements
iOS 13.0+
Installation
CompositionalLayoutViewController is available through CocoaPods. To install it, simply add the following line to your Podfile:
Usage
There are four steps to declare collection view.
Firstly, to compose complex collection view sections, you implement
Section
class that inheritsCollectionViewSection
.Secondly, implement your sections layout.
Thirdly, register cells and supplementary views for collection view in
func registerCell(collectionView: UICollectionView)
andfunc registerSupplementaryView(collectionView: UICollectionView)
.Make sure that you implement
func supplementaryView(_ collectionView: UICollectionView, kind: String, indexPath: IndexPath) -> UICollectionReusableView?
. You may have to dequeUICollectionReusableView
from a collection view.These cells and supplementary views are configured following methods.
You also have to declare
var snapshotItems: [AnyHashable]
in your section class. This variable contains datas for each cells.Finally, you can declare collection view sections in subclass of
CompositionalLayoutViewController
as follows:And also don’t forget to assign
SectionProvider
that manages an array of sections.To handle cell selection, override
func didSelectItem(at indexPath: IndexPath)
method in subclass ofCompositionalLayoutViewController
.See example code to lean advanced usage.
Author
Akira, akira.matsuda@me.com
License
CompositionalLayoutViewController is available under the MIT license. See the LICENSE file for more info.