How to Create a Section with SKCSingleTypeSection in SectionKit
SKCSingleTypeSection is the core generic class in SectionKit for rendering a list whose rows all use the same cell type, requiring only that your cell conforms to UICollectionViewCell, SKConfigurableView, and SKLoadViewProtocol.
The SKCSingleTypeSection class from the linhay/sectionkit repository provides a type-safe, fluent API for building UICollectionView sections. It eliminates boilerplate by binding a single cell type to a section while handling layout, data binding, and interaction callbacks automatically.
Understanding the Generic Constraints
SKCSingleTypeSection is defined in Sources/SectionKit/CollectionSingleTypeSection/Entities/SKCSingleTypeSection.swift (line 12) as a generic class with strict protocol constraints. Your cell must implement three protocols to work with this section:
UICollectionViewCell– The standard UIKit base class.SKConfigurableView– Provides theconfig(_ model:)method for data binding.SKLoadViewProtocol– Ensures the cell can load from a nib or be created programmatically.
The compiler infers the Model type from the cell’s associated type, guaranteeing type safety between your data array and the cell’s configuration method.
Instantiating a Section
Creating the Section Instance
To create a section with SKCSingleTypeSection, specify your cell type as the generic parameter:
import SectionKit
import UIKit
let section = SKCSingleTypeSection<DemoCell>()
This single line fixes the cell type for the entire section. The section now knows exactly which cell class to dequeue and which model type to expect.
Configuring Layout Properties
The class exposes mutable properties for UICollectionViewFlowLayout customization directly on the instance:
section.minimumLineSpacing = 10
section.minimumInteritemSpacing = 10
section.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
section.itemSize = CGSize(width: UIScreen.main.bounds.width - 20, height: 44)
These properties map directly to the collection view layout, allowing you to adjust spacing and insets without implementing delegate methods.
Providing and Managing Data
Setting the Data Source
Use the config(models:) method (defined at line 473 in SKCSingleTypeSection.swift) to populate the section:
struct DemoCell: UICollectionViewCell, SKConfigurableView, SKLoadViewProtocol {
struct Model { let title: String }
func config(_ model: Model) {
textLabel.text = model.title
}
static var nibName: String { "DemoCell" }
}
let models = (1...20).map { DemoCell.Model(title: "Row \($0)") }
section.config(models: models)
Incremental Updates
For adding items after creation, use the fluent API:
append(_ model: Model)– Adds a single item to the end.insert(_ model: Model, at index: Int)– Inserts at a specific position.
Both methods return Self, enabling chaining with other configuration calls.
Styling and Interaction Handling
Customizing Section Style
The file SKCSingleTypeSection+onAction.swift provides setSectionStyle (line 30) for custom styling via closures:
section.setSectionStyle(\.itemSize) {
CGSize(width: UIScreen.main.bounds.width - 40, height: 60)
}
This uses weak-reference overloads to prevent retain cycles while allowing dynamic property calculation.
Handling Cell Actions
Cell interactions are exposed through onCellAction callbacks that deliver a SKCSingleTypeSectionCellActionContext (defined in SKCSingleTypeCellActionContextProtocol.swift, line 12). This context provides the cell, model, and index path for the triggered action.
Integration with SKCManager
After configuring the section, add it to an SKCManager (or any SKCollectionView):
let manager = SKCManager(collectionView: myCollectionView)
manager.append(section)
Alternatively, replace the entire section array:
manager.sections = [section]
Complete Working Example
Following the pattern from MainViewController.swift (line 118) and SingleTypeSectionViewController.swift (lines 17-28), here is a production-ready implementation:
import SectionKit
import UIKit
final class MenuCell: UICollectionViewCell, SKConfigurableView, SKLoadViewProtocol {
struct Model { let title: String; let subtitle: String? }
func config(_ model: Model) {
textLabel.text = model.title
detailTextLabel.text = model.subtitle
}
static var nibName: String { "MenuCell" }
}
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
private lazy var manager = SKCManager(collectionView: collectionView)
override func viewDidLoad() {
super.viewDidLoad()
setupSection()
}
private func setupSection() {
let section = SKCSingleTypeSection<MenuCell>()
section.minimumLineSpacing = 8
section.sectionInset = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
let models = [
MenuCell.Model(title: "Dashboard", subtitle: "View stats"),
MenuCell.Model(title: "Settings", subtitle: "Adjust preferences")
]
section.config(models: models)
section.headerTitle = "Main Menu"
manager.append(section)
}
}
Summary
- SKCSingleTypeSection is a generic class in
Sources/SectionKit/CollectionSingleTypeSection/Entities/SKCSingleTypeSection.swiftthat binds a single cell type to a collection view section. - Cells must conform to
UICollectionViewCell,SKConfigurableView, andSKLoadViewProtocolto be used with this section type. - Use
config(models:)to set data, and manipulate layout properties likeminimumLineSpacingandsectionInsetdirectly on the instance. - Attach the configured section to an
SKCManagerviaappend(section)or by setting thesectionsarray. - The
setSectionStylemethod provides a closure-based API for dynamic styling without subclassing.
Frequently Asked Questions
What protocols must a cell conform to for SKCSingleTypeSection?
Your cell must implement three protocols: UICollectionViewCell as the base class, SKConfigurableView (which adds the config(_ model:) binding method), and SKLoadViewProtocol (which specifies how to load the cell from a nib or programmatically). The model type is automatically inferred from the cell’s Model associated type.
How do I update data in an existing SKCSingleTypeSection?
Call config(models:) to replace the entire dataset, or use append(_:) and insert(_:at:) for incremental updates. For reload operations, see SKCSingleTypeSection+refresh.swift which provides reload() and refresh() methods to trigger collection view updates.
Can I use SKCSingleTypeSection with multiple cell types?
No. By design, SKCSingleTypeSection enforces a single cell type per section via its generic parameter. If you need heterogeneous cell types within one section, you would use a different section implementation from the SectionKit framework that supports type erasure or multiple cell registration.
Where is the SKCSingleTypeSection class defined in the source code?
The core class definition resides in Sources/SectionKit/CollectionSingleTypeSection/Entities/SKCSingleTypeSection.swift at line 12. Additional functionality is split across SKCSingleTypeSection+onAction.swift (styling helpers) and SKCSingleTypeSection+refresh.swift (reload methods). Example usage appears in Example/MainViewController.swift (line 118) and Example/Foundation/SingleTypeSectionViewController.swift (lines 17-28).
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 →