Contents

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 : Sendable

Overview

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:

  1. Connect a diffable data source to your collection view.

  2. Implement a cell provider to configure your collection view’s cells.

  3. Generate the current state of the data.

  4. 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

Updating section data

Supporting reordering

Supporting expanding and collapsing

Debugging a diffable data source

Supporting bridging

See Also

Data