UICollectionViewDiffableDataSource
The object you use to manage data and provide cells for a collection view.
Declaration
@MainActor @preconcurrency class UICollectionViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType> where SectionIdentifierType : Hashable, SectionIdentifierType : Sendable, ItemIdentifierType : Hashable, ItemIdentifierType : SendableOverview
A diffable data source object is a specialized type of data source that works together with your collection view object. It provides the behavior you need to manage updates to your collection view’s data and UI in a simple, efficient way. It also conforms to the UICollectionViewDataSource protocol and provides implementations for all of the protocol’s methods.
To fill a collection view with data:
Connect a diffable data source to your collection view.
Implement a cell provider to configure your collection view’s cells.
Generate the current state of the data.
Display the data in the UI.
To connect a diffable data source to a collection view, you create the diffable data source using its init(collectionView:cellProvider:) initializer, passing in the collection view you want to associate with that data source. You also pass in a cell provider, where you configure each of your cells to determine how to display your data in the UI.
dataSource = UICollectionViewDiffableDataSource<Int, UUID>(collectionView: collectionView) {
(collectionView: UICollectionView, indexPath: IndexPath, itemIdentifier: UUID) -> UICollectionViewCell? in
// Configure and return cell.
}Then, you generate the current state of the data and display the data in the UI by constructing and applying a snapshot. For more information, see NSDiffableDataSourceSnapshot.
Topics
Creating a diffable data source
Creating supplementary views
Identifying items
Identifying sections
Updating data
snapshot()apply(_:animatingDifferences:)apply(_:animatingDifferences:completion:)applySnapshotUsingReloadData(_:)applySnapshotUsingReloadData(_:completion:)
Updating section data
Supporting reordering
reorderingHandlersUICollectionViewDiffableDataSource.ReorderingHandlersNSDiffableDataSourceTransactionNSDiffableDataSourceSectionTransaction