Skip to content

Commit e6c7fb4

Browse files
committed
fixed SupplimentoryView private issue and added SupplimentoryView to multisession example demo
1 parent 10d2560 commit e6c7fb4

File tree

5 files changed

+68
-43
lines changed

5 files changed

+68
-43
lines changed

GenericDataSource/Cells/HeaderFooterReusableView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
import UIKit
99

10-
class HeaderFooterReusableView: UICollectionReusableView {
10+
class HeaderFooterReusableView: UICollectionReusableView, NibReusable {
1111

1212
@IBOutlet var headerLabel: UILabel!
1313

1414
override func awakeFromNib() {
1515
super.awakeFromNib()
16-
// Initialization code
16+
backgroundColor = .lightGray
1717
}
1818

1919
func render(with header: String) {

GenericDataSource/Cells/HeaderFooterReusableView.xib

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<objects>
1111
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
1212
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
13-
<collectionReusableView opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" id="U6b-Vx-4bR">
13+
<collectionReusableView opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" id="U6b-Vx-4bR" customClass="HeaderFooterReusableView" customModule="GenericDataSource" customModuleProvider="target">
1414
<rect key="frame" x="0.0" y="0.0" width="320" height="50"/>
1515
<autoresizingMask key="autoresizingMask"/>
1616
<subviews>
@@ -27,6 +27,9 @@
2727
<constraint firstItem="1r3-Wd-C5y" firstAttribute="centerY" secondItem="U6b-Vx-4bR" secondAttribute="centerY" id="NKu-kK-xHS"/>
2828
<constraint firstAttribute="trailing" secondItem="1r3-Wd-C5y" secondAttribute="trailing" constant="20" symbolic="YES" id="pUZ-b0-hZS"/>
2929
</constraints>
30+
<connections>
31+
<outlet property="headerLabel" destination="1r3-Wd-C5y" id="fP1-5G-OHG"/>
32+
</connections>
3033
<point key="canvasLocation" x="139" y="129"/>
3134
</collectionReusableView>
3235
</objects>

GenericDataSource/DataSource/CollectionDiffableDataSource/DiffableCollectionViewDataSource.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ open class DiffableCollectionViewDataSource<Section: SectionType, Item>: NSObjec
2727
private var sections: [CellSection] = []
2828
private weak var collectionView: UICollectionView?
2929
private var cellProvider: (UICollectionView, IndexPath, Item) -> UICollectionViewCell?
30-
private var supplementaryViewProvider: DiffableCollectionViewDataSource<Section, Item>.SupplementaryViewProvider?
30+
31+
public var supplementaryViewProvider: DiffableCollectionViewDataSource<Section, Item>.SupplementaryViewProvider?
3132

3233
// MARK: init
3334

GenericDataSource/Extensions/Reusable+Extensions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public extension UICollectionView {
6363

6464

6565
//MARK: UICollectionReusableView
66-
final func register<T: UICollectionReusableView>(supplementaryViewType: T.Type, ofKind elementKind: String)
66+
final func register<T: UICollectionReusableView>(_ supplementaryViewType: T.Type, ofKind elementKind: String)
6767
where T: NibReusable {
6868
self.register(
6969
supplementaryViewType.nib,
@@ -72,7 +72,7 @@ public extension UICollectionView {
7272
)
7373
}
7474

75-
final func register<T: UICollectionReusableView>(supplementaryViewType: T.Type, ofKind elementKind: String)
75+
final func register<T: UICollectionReusableView>(_ supplementaryViewType: T.Type, ofKind elementKind: String)
7676
where T: Reusable {
7777
self.register(
7878
supplementaryViewType.self,

GenericDataSource/ViewController/MultiSection/MultiSectionCollectionViewController.swift

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)