The cellProvider must be overwritten because the default implementation raises a fatal error.
Do not forget to register the cell before you call applySnapshot(_:) the first time!
The cell selection can be implemented like that:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = dataSource.itemIdentifier(for: indexPath)
/*do your actions with the selected item*/
}
Customize Header and Footer Views (collection view only)
In order to use custom header or footer views, the supplementaryViewProvider property must be overwritten.
override var supplementaryViewProvider: UICollectionViewDiffableDataSource<Section, Item>.SupplementaryViewProvider? {
return { collectionView, kind, indexPath in
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "customHeader", for: indexPath) as? CustomHeader
switch self.dataSource.snapshot().sectionIdentifiers[indexPath.section] {
case .main:
headerView?.backgroundColor = .systemGreen
case .detail:
headerView?.backgroundColor = .systemPink
}
return headerView
}
}
Do not forget to register the supplementary view before you call applySnapshot(_:) the first time!
In case you want to change the behavior of the title creation or you want to use section index titles and other features you have to subclass the appropriate data source. For the table view it could look something like that:
class CustomDataSource: UITableViewDiffableDataSource<Section, Item> {
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch snapshot().sectionIdentifiers[section] {
case .main:
return "Main"
case .detail:
return "Detail"
}
}
}
Use UICollectionViewDiffableDataSource for collection view.
So that it can be used in our view controller we have to create a lazy var of this and override the dataSource property with it.
lazy var customDataSource = CustomDataSource(tableView: tableView, cellProvider: cellProvider)
override var dataSource: UITableViewDiffableDataSource<Section, Item> {
return customDataSource
}
Custom applySnapshot(_:)
You are not forced to use applySnapshot(_:) in combination with FHDiffableDataSourceSection array to apply snapshots to your dataSource. You can override the applySnapshot(_:) to change its behavior, create your own applySnapshot() method or do it manually as follows:
FHDiffableViewControllers
UITableViewController and UICollectionViewController based on a DiffableDataSource.
Requirements
Installation
Swift Package Manager
Add the following to the dependencies of your
Package.swift
:Xcode
Add the package to your project as shown here.
Manual
Download the files in the Sources folder and drag them into you project.
Usage
If you are using Swift Package Manager, you have to import FHDiffableViewControllers to your file with
import FHDiffableViewControllers
.Now you can inherit your view controller class from
FHDiffableTableViewController
andFHDiffableCollectionViewController
.But first you need a
SectionIdentifier
andItemIdentifier
which will be used for the generic types. Both types have to conform toHashable
.These classes can be subclassed like that:
The cell selection can be implemented like that:
Customize Header and Footer Views (collection view only)
In order to use custom header or footer views, the
supplementaryViewProvider
property must be overwritten.Do not forget to register the supplementary view before you call
applySnapshot(_:)
the first time!Custom Data Source
In case you want to change the behavior of the title creation or you want to use section index titles and other features you have to subclass the appropriate data source. For the table view it could look something like that:
So that it can be used in our view controller we have to create a lazy var of this and override the
dataSource
property with it.Custom
applySnapshot(_:)
You are not forced to use
applySnapshot(_:)
in combination withFHDiffableDataSourceSection
array to apply snapshots to yourdataSource
. You can override theapplySnapshot(_:)
to change its behavior, create your ownapplySnapshot()
method or do it manually as follows:Section & Item builder
In addition to the traditional Array way there is a result builder for both the Section’s and Item’s creation.
Array
Result Builder
License
FHConstraints is available under the MIT license. See the LICENSE file for more info.