Skip to content

Commit 32a198d

Browse files
authored
Merge pull request #3 from alexjameslittle/master
SupplementaryViewFactory for section headers and footers
2 parents 1049b68 + c75e00f commit 32a198d

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Sources/CombineDataSources/CollectionView/CollectionViewItemsController.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class CollectionViewItemsController<CollectionType>: NSObject, UICollecti
1818

1919
public typealias Element = CollectionType.Element.Element
2020
public typealias CellFactory<Element: Equatable> = (CollectionViewItemsController<CollectionType>, UICollectionView, IndexPath, Element) -> UICollectionViewCell
21+
public typealias SupplementaryViewFactory = (CollectionViewItemsController<CollectionType>, UICollectionView, String, IndexPath, CollectionType.Element) -> UICollectionReusableView
2122
public typealias CellConfig<Element, Cell> = (Cell, IndexPath, Element) -> Void
2223

2324
private let cellFactory: CellFactory<Element>
@@ -31,6 +32,8 @@ public class CollectionViewItemsController<CollectionType>: NSObject, UICollecti
3132

3233
/// A fallback data source to implement custom logic like indexes, dragging, etc.
3334
public var dataSource: UICollectionViewDataSource?
35+
36+
public var configureSupplementaryView: SupplementaryViewFactory?
3437

3538
// MARK: - Init
3639

@@ -97,6 +100,13 @@ public class CollectionViewItemsController<CollectionType>: NSObject, UICollecti
97100
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
98101
cellFactory(self, collectionView, indexPath, collection[indexPath.section][indexPath.row])
99102
}
103+
104+
public func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
105+
guard let configureSupplementaryView = configureSupplementaryView else {
106+
fatalError("Property `configureSupplementaryView` must not be nil when using supplementary views")
107+
}
108+
return configureSupplementaryView(self, collectionView, kind, indexPath, collection[indexPath.section])
109+
}
100110

101111
// MARK: - Fallback data source object
102112
override public func forwardingTarget(for aSelector: Selector!) -> Any? {

0 commit comments

Comments
 (0)