Skip to content

SupplementaryViewFactory for section headers and footers #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class CollectionViewItemsController<CollectionType>: NSObject, UICollecti

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

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

/// A fallback data source to implement custom logic like indexes, dragging, etc.
public var dataSource: UICollectionViewDataSource?

public var configureSupplementaryView: SupplementaryViewFactory?

// MARK: - Init

Expand Down Expand Up @@ -97,6 +100,13 @@ public class CollectionViewItemsController<CollectionType>: NSObject, UICollecti
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cellFactory(self, collectionView, indexPath, collection[indexPath.section][indexPath.row])
}

public func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
guard let configureSupplementaryView = configureSupplementaryView else {
fatalError("Property `configureSupplementaryView` must not be nil when using supplementary views")
}
return configureSupplementaryView(self, collectionView, kind, indexPath, collection[indexPath.section])
}

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