@@ -45,57 +45,29 @@ class MultiSectionCollectionViewController: UICollectionViewController {
4545 required init ? ( coder: NSCoder ) {
4646 fatalError ( " init(coder:) has not been implemented " )
4747 }
48-
49- fileprivate func configureDataSource( ) {
50-
51- dataSource = DiffableCollectionViewDataSource < Section , AnyDifferentiable > (
52- collectionView: collectionView,
53- cellProvider: { ( collectionView, indexPath, anyDifferentiable) -> UICollectionViewCell ? in
54- let section : CellSection = self . dataInput [ indexPath. section] . model
55- switch section {
56- case . vertical:
57- let cell = collectionView. dequeueReusableCell ( for: indexPath) as VerticalCell
58- if let carousel = anyDifferentiable. base as? Carousel {
59- cell. render ( with: carousel)
60- }
61- return cell
62- case . horizontal:
63- let cell = collectionView. dequeueReusableCell ( for: indexPath) as HorizontalCell
64- if let playlist = anyDifferentiable. base as? Playlist {
65- cell. render ( with: playlist)
66- }
67- return cell
68- case . customText:
69- let cell = collectionView. dequeueReusableCell ( for: indexPath) as CustomTextCell
70- if let ads = anyDifferentiable. base as? Ads {
71- cell. render ( with: ads)
72- }
73- return cell
74- }
75- }
76- )
77-
78- let reusableView =
79- }
80-
48+
8149 override func viewDidLoad( ) {
8250 super. viewDidLoad ( )
8351
84- collectionView. backgroundColor = . systemBackground
85- navigationItem. title = " MultiSection Demo "
86- collectionView. delegate = self
87-
52+ setupViews ( )
8853 setupBarButtons ( )
8954 registerCells ( )
9055 configureDataSource ( )
9156 setupData ( )
9257 }
9358
59+ private func setupViews( ) {
60+ collectionView. backgroundColor = . systemBackground
61+ navigationItem. title = " MultiSection Demo "
62+ collectionView. delegate = self
63+ }
64+
9465 private func registerCells( ) {
9566 // Register cell classes
9667 collectionView. register ( VerticalCell . self)
9768 collectionView. register ( HorizontalCell . self)
9869 collectionView. register ( CustomTextCell . self)
70+ collectionView. register ( HeaderFooterReusableView . self, ofKind: UICollectionView . elementKindSectionHeader)
9971 }
10072
10173 private func setupData( ) {
@@ -131,6 +103,47 @@ class MultiSectionCollectionViewController: UICollectionViewController {
131103 }
132104 }
133105
106+ // MARK: DiffableCollectionViewDataSource
107+
108+ fileprivate func configureDataSource( ) {
109+
110+ dataSource = DiffableCollectionViewDataSource < Section , AnyDifferentiable > (
111+ collectionView: collectionView,
112+ cellProvider: { ( collectionView, indexPath, anyDifferentiable) -> UICollectionViewCell ? in
113+ let section : CellSection = self . dataInput [ indexPath. section] . model
114+ switch section {
115+ case . vertical:
116+ let cell = collectionView. dequeueReusableCell ( for: indexPath) as VerticalCell
117+ if let carousel = anyDifferentiable. base as? Carousel {
118+ cell. render ( with: carousel)
119+ }
120+ return cell
121+ case . horizontal:
122+ let cell = collectionView. dequeueReusableCell ( for: indexPath) as HorizontalCell
123+ if let playlist = anyDifferentiable. base as? Playlist {
124+ cell. render ( with: playlist)
125+ }
126+ return cell
127+ case . customText:
128+ let cell = collectionView. dequeueReusableCell ( for: indexPath) as CustomTextCell
129+ if let ads = anyDifferentiable. base as? Ads {
130+ cell. render ( with: ads)
131+ }
132+ return cell
133+ }
134+ }
135+ )
136+
137+ dataSource. supplementaryViewProvider = { ( collectionView, kind, indexPath) in
138+ guard indexPath. section > 0 else {
139+ return nil
140+ }
141+ let headerView = collectionView. dequeueReusableSupplementaryView ( ofKind: kind, for: indexPath) as HeaderFooterReusableView
142+ headerView. render ( with: " HeaderTitle: Section- \( indexPath. section) " )
143+ return headerView
144+ }
145+ }
146+
134147 // MARK: Private methods
135148
136149 fileprivate func setupBarButtons( ) {
@@ -191,4 +204,12 @@ extension MultiSectionCollectionViewController: UICollectionViewDelegateFlowLayo
191204 return CGSize ( width: view. frame. width, height: 98 )
192205 }
193206 }
207+
208+ func collectionView( _ collectionView: UICollectionView ,
209+ layout collectionViewLayout: UICollectionViewLayout ,
210+ referenceSizeForHeaderInSection section: Int ) -> CGSize {
211+ guard section > 0 else { return . zero }
212+ return CGSize ( width: UIScreen . main. bounds. width, height: 50 )
213+ }
214+
194215}
0 commit comments