SectionKit SKCSingleTypeSection: Building Type-Safe Single-Cell Collection Sections
SKCSingleTypeSection is a generic, single-cell-type section implementation in SectionKit designed for homogeneous content lists where every row uses the same UICollectionViewCell subclass and shares a common data model.
SKCSingleTypeSection eliminates boilerplate when building collection views with uniform rows. According to the linhay/sectionkit source code, this class provides a complete, type-safe infrastructure for data binding, diff-based reloads, and cell lifecycle management without requiring a custom UICollectionViewDataSource.
What is SKCSingleTypeSection?
SKCSingleTypeSection is an open class that manages a section of collection view cells where every item is rendered by the same cell type. In SKCSingleTypeSection.swift lines 12-13, the class is declared with three generic constraints:
open class SKCSingleTypeSection<Cell: UICollectionViewCell & SKConfigurableView & SKLoadViewProtocol>
This declaration ties the section to a specific cell type that must conform to SKConfigurableView (providing a Model type and configuration method) and SKLoadViewProtocol (providing size calculation). The section uses typealias Model = Cell.Model to ensure type consistency between the data array and cell configuration.
Architecture and Conformance
According to SKCSingleTypeSectionProtocol.swift lines 11-14, the section conforms to SKCSingleTypeSectionProtocol, which inherits from:
SKCSectionProtocol– Base section behaviorSKCViewDataSourcePrefetchingProtocol– Cell prefetching supportSKSafeSizeProviderProtocol– Safe area size handling
Internally, the class manages an array of Model objects, publishes changes via Combine, handles high-performance size caching through highPerformance/highPerformanceID properties, and supports flexible reload strategies via the ReloadKind enum defined in SKCSingleTypeSection.swift lines 35-39.
When to Use SKCSingleTypeSection
Use this class when you need a ready-made, type-safe section for collections with homogeneous content. The primary use cases include:
- Homogeneous lists – Every row uses the same cell class (text lists, image grids, card collections)
- Type-safe data binding – You want compile-time guarantees that data models match cell expectations without manual casting
- Advanced reload semantics – You need automatic diff-based updates via
apply(models:)or fine-grained reload control withreloadKindoptions like.differenceor.configAndDelete - Built-in infrastructure – You require prefetching, context menus, cell actions, and high-performance size caching without writing custom delegate methods
Avoid SKCSingleTypeSection when a section contains heterogeneous cell types; instead, implement a custom section conforming to SKCSectionProtocol directly.
Implementing SKCSingleTypeSection
Creating a Reusable Cell
First, define a cell that conforms to SKConfigurableView and SKLoadViewProtocol. In Sources/SectionKit/SKConfigurable/, these protocols require a Model struct, a config(_:) method, and a static size calculation method.
import UIKit
import SectionKit
final class TextCell: UICollectionViewCell, SKConfigurableView, SKLoadViewProtocol {
struct Model {
let text: String
}
private let label = UILabel()
// MARK: - SKConfigurableView
func config(_ model: Model) {
label.text = model.text
}
// MARK: - SKLoadViewProtocol
static func preferredSize(limit: CGSize, model: Model) -> CGSize {
return CGSize(width: limit.width, height: 44)
}
}
The SKConfigurableView protocol ensures the cell can be configured with its associated Model type, while SKLoadViewProtocol enables the section to calculate cell sizes before instantiation.
Initializing the Section
Initialize the section with an array of models. The init(_ models:) method referenced in SKCSingleTypeSection.swift lines 51-53 accepts the initial data array:
let items = [
TextCell.Model(text: "First Item"),
TextCell.Model(text: "Second Item"),
TextCell.Model(text: "Third Item")
]
let textSection = SKCSingleTypeSection<TextCell>(items)
You can customize cell appearance and handle interactions using the section's modifier methods:
// Apply conditional styling
textSection.if({ true }) { section in
section.cellStyles.append { context in
context.view.backgroundColor = .systemBackground
}
}
// Handle cell selection
textSection.onCellAction(.selected) { context in
print("Selected: \(context.model.text)")
}
Wiring to SKCManager
SKCManager orchestrates the collection view. Assign your section to the manager's sections array and call reloadData():
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
let manager = SKCManager(collectionView: collectionView)
manager.sections = [textSection]
manager.reloadData()
SKCManager (defined in SKCManager.swift lines 62-85) forwards all data source and delegate calls to each section conforming to SKCAnySectionProtocol.
Advanced Diff-Based Reloads
For efficient updates, use the apply(models:) method with a custom equivalence closure. The implementation in SKCSingleTypeSection.swift uses the reloadKind property to determine the update strategy:
// Configure diff-based reloading
textSection.reloadKind = .difference { $0.text == $1.text }
// Apply new data with automatic insert/delete/move calculations
let newItems = [
TextCell.Model(text: "First Item"),
TextCell.Model(text: "Fourth Item") // Changed from "Third Item"
]
textSection.apply(newItems)
The ReloadKind enum supports strategies ranging from full reloads to configuration-only updates, implemented in the private reload(models:by:) method (lines 44-62).
Key Source Files and Components
Understanding the file structure helps when debugging or extending functionality:
SKCSingleTypeSection.swift– Core implementation containing the generic class declaration (lines 12-13), initialization (lines 51-53),apply(models:)(lines 59-67), andconfig(models:)(lines 71-75)SKCSingleTypeSectionProtocol.swift– Defines the protocol requirements and associated types tying cells to sections (lines 11-18)Plugin+SKCSingleTypeSection.swift– Bridges the section to the UI layer and collection viewSources/SectionKit/SKConfigurable/– ContainsSKConfigurableViewandSKLoadViewProtocolrequired by compatible cells
Summary
SKCSingleTypeSectionis a generic, open class in SectionKit for managing collection view sections with uniform cell types- It requires cells to conform to
SKConfigurableViewandSKLoadViewProtocolfor type-safe data binding and size calculation - Use it for homogeneous content to gain automatic diffing, prefetching, context menus, and high-performance size caching without custom data sources
- Key methods include
init(_ models:),apply(models:)for diff updates, andonCellAction(_:)for event handling - The class supports flexible reload strategies via the
ReloadKindenum including difference-based updates
Frequently Asked Questions
What protocols must a cell implement to work with SKCSingleTypeSection?
A cell must conform to SKConfigurableView and SKLoadViewProtocol. SKConfigurableView requires an associated Model type and a config(_:) method for data binding, while SKLoadViewProtocol requires preferredSize(limit:model:) for size calculation. These protocols are defined in the Sources/SectionKit/SKConfigurable/ directory.
How does SKCSingleTypeSection handle cell size calculation?
The section delegates size calculation to the cell's static method defined in SKLoadViewProtocol. It also provides high-performance caching through highPerformance and highPerformanceID properties, which cache calculated sizes to avoid expensive recomputation during scrolling.
Can I use SKCSingleTypeSection with multiple cell types in one section?
No. SKCSingleTypeSection is strictly designed for single-type sections where every row uses the same cell class. For heterogeneous sections with multiple cell types, implement a custom section conforming to SKCSectionProtocol directly.
What is the difference between apply(models:) and config(models:)?
apply(models:) performs a diff-aware reload using the section's reloadKind strategy (such as .difference), calculating minimal insertions, deletions, and moves. config(models:) simply replaces the internal model array and reloads the section without diffing, as implemented in SKCSingleTypeSection.swift lines 71-75.
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 →