What is SKCSectionProtocol in SectionKit? Protocol Definition and Implementation Guide
SKCSectionProtocol is the central typealias in SectionKit that aggregates base section functionality, flow layout configuration, and type erasure to define a complete, self-contained section in a data-driven UICollectionView architecture.
SKCSectionProtocol serves as the foundational contract for building modular collection view sections in the SectionKit framework. Defined in Sources/SectionKit/CollectionBaseProtocol/SKCSectionProtocol.swift, this protocol combines multiple specialized protocols to provide a single interface for managing cell creation, layout calculations, and user interactions.
SKCSectionProtocol Definition and Type Composition
At its core, SKCSectionProtocol is a compound typealias that composes three distinct protocol requirements into one cohesive interface. The definition as found in Sources/SectionKit/CollectionBaseProtocol/SKCSectionProtocol.swift is:
public typealias SKCSectionProtocol = SKCBaseSectionProtocol &
SKCViewDelegateFlowLayoutProtocol &
SKCAnySectionProtocol
This composition ensures that any conforming type provides complete functionality for rendering content, calculating layouts, and handling interactions within a UICollectionView. By aggregating these three distinct concerns into a single protocol, SectionKit enables sections to act as autonomous units that encapsulate all their configuration and behavior.
Core Components of SKCSectionProtocol
The protocol aggregates three primary components, each defined in separate source files that describe specific section responsibilities.
SKCBaseSectionProtocol
SKCBaseSectionProtocol itself composes three smaller protocols to handle fundamental data and interaction logic. According to the source code in Sources/SectionKit/CollectionBase/SKCSectionActionProtocol.swift, Sources/SectionKit/CollectionBaseProtocol/SKCDataSource/SKCDataSourceProtocol.swift, and Sources/SectionKit/CollectionBaseProtocol/SKCDelegate/SKCDelegateProtocol.swift, this component includes:
- Section actions: Methods for
reload(),insert(),remove(), and scroll operations - Data source conformance: Required methods like
item(at:)anditemCount, plus optional supplementary view support - Delegate callbacks: Selection, highlighting, display, and context menu lifecycle events
SKCViewDelegateFlowLayoutProtocol
Located in Sources/SectionKit/CollectionBaseProtocol/SKCDelegateFlowLayout/SKCViewDelegateFlowLayoutProtocol.swift, this component supplies all layout-related information for flow layout configuration. It enables per-section customization of item sizes, header and footer dimensions, minimum spacing, and section insets without requiring a separate layout delegate object.
SKCAnySectionProtocol
Defined in Sources/SectionKit/CollectionBaseProtocol/SKCAnySectionProtocol.swift, this protocol provides the type erasure mechanism essential for heterogeneous section collections. It exposes a unique ObjectIdentifier and the underlying section instance, allowing SKCManager to store [any SKCSectionProtocol] arrays while maintaining access to concrete section APIs.
Architectural Role in SectionKit
SKCSectionProtocol enables several architectural patterns that simplify collection view management.
Single Source of Truth
Each section object implementing SKCSectionProtocol becomes the definitive source for its data, appearance, and behavior. The section encapsulates everything required for the collection view to render its rows, eliminating index-path-based logic from view controllers and centralizing section-specific concerns within the section itself.
Composable Design
Because SKCSectionProtocol builds from smaller, focused protocols, developers can implement only the functionality they need. Most concrete sections, however, adopt SKCSectionProtocol directly and rely on protocol extensions for default implementations of standard methods like basic data source callbacks.
Type-Erased Section Management
The inclusion of SKCAnySectionProtocol allows the manager class to handle heterogeneous collections. As implemented in Sources/SectionKit/CollectionBase/SKCManager.swift, the manager stores an array of any SKCSectionProtocol and forwards collection view data source and delegate calls to the appropriate section based on index path calculations.
Practical Implementation Examples
Creating Sections with SKCSingleTypeSection
Most developers utilize SKCSingleTypeSection, a generic concrete type that conforms to SKCSectionProtocol. This class provides convenience methods for single-cell-type sections while handling the underlying protocol requirements automatically. From Example/MainViewController.swift:
private func makeSection(title: String, models: [MenuCell.Model]) -> SKCSingleTypeSection<MenuCell> {
return MenuCell.wrapperToSingleTypeSection(models)
.onCellAction(.selected) { [weak self] context in
let vc = context.model.action()
self?.navigationController?.pushViewController(vc, animated: true)
}
.setHeader(HeaderLabel.self, model: title) { view in
view.backgroundColor = .secondarySystemBackground
}
}
Registering Sections with SKCManager
Sections are registered with the manager using the reload method, which accepts an array of any SKCSectionProtocol. This pattern, demonstrated in Example/MainViewController.swift, allows mixing different section types in a single collection view:
manager.reload([
makeFoundationSection(),
makeLayoutSection(),
makeDataSection(),
makeInteractionSection(),
makePageSection(),
])
Customizing Layout Behavior
Developers can customize per-section layout by implementing methods from SKCViewDelegateFlowLayoutProtocol. This approach leverages the layout component of SKCSectionProtocol to provide fine-grained control:
extension MySection: SKCSectionProtocol {
func itemSize(at row: Int) -> CGSize {
.init(width: view.bounds.width, height: 44)
}
var headerSize: CGSize {
.init(width: view.bounds.width, height: 60)
}
}
Summary
- SKCSectionProtocol is a typealias aggregating
SKCBaseSectionProtocol,SKCViewDelegateFlowLayoutProtocol, andSKCAnySectionProtocoldefined inSources/SectionKit/CollectionBaseProtocol/SKCSectionProtocol.swift. - It serves as the single source of truth for collection view sections, encapsulating data source methods, delegate callbacks, layout configuration, and action handling.
- The protocol enables modular, reusable sections through compositional design and type-erased storage via
SKCAnySectionProtocolfor heterogeneous collections. - Concrete implementations like
SKCSingleTypeSectionprovide ready-to-use conformance for common single-cell-type use cases. SKCManagerstores arrays ofany SKCSectionProtocoland forwards collection view callbacks to appropriate sections based on index paths.
Frequently Asked Questions
How does SKCSectionProtocol differ from standard UICollectionView protocols?
Unlike standard UICollectionViewDataSource and UICollectionViewDelegate which require index-path-based switch statements in a view controller, SKCSectionProtocol encapsulates all section-specific behavior within the section object itself. This eliminates fragile index-path calculations and allows sections to be reused across different collection views without modifying view controller code, as demonstrated in the SKCManager implementation.
Can I create a custom section without using SKCSingleTypeSection?
Yes, you can create a custom type that conforms directly to SKCSectionProtocol by implementing the requirements from SKCBaseSectionProtocol, SKCViewDelegateFlowLayoutProtocol, and SKCAnySectionProtocol. However, most developers use SKCSingleTypeSection or subclass existing section types because they provide default implementations for the majority of protocol requirements through protocol extensions.
What is the purpose of SKCAnySectionProtocol within SKCSectionProtocol?
SKCAnySectionProtocol provides type erasure that allows SKCManager to maintain arrays of heterogeneous sections. It exposes the underlying section instance and a unique ObjectIdentifier, enabling the manager to store [any SKCSectionProtocol] while still accessing section-specific functionality. This pattern is crucial for supporting mixed section types in a single collection view manager.
Where can I find the default implementations for SKCSectionProtocol methods?
Default implementations are distributed across protocol extension files in the Sources/SectionKit/CollectionBaseProtocol/ directory. Key files include SKCSectionActionProtocol.swift for reload and insert methods, SKCDataSourceProtocol.swift for cell creation defaults, and SKCDelegateProtocol.swift for selection handling. These extensions minimize boilerplate code while allowing customization of specific behaviors.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →