UITableViewDiffableDataSource
The object you use to manage data and provide cells for a table view.
Declaration
@MainActor @preconcurrency class UITableViewDiffableDataSource<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 table view object. It provides the behavior you need to manage updates to your table view’s data and UI in a simple, efficient way. It also conforms to the UITableViewDataSource protocol and provides implementations for all of the protocol’s methods.
To fill a table view with data:
Connect a diffable data source to your table view.
Implement a cell provider to configure your table view’s cells.
Generate the current state of the data.
Display the data in the UI.
To connect a diffable data source to a table view, you create the diffable data source using its init(tableView:cellProvider:) initializer, passing in the table 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 = UITableViewDiffableDataSource<Int, UUID>(tableView: tableView) {
(tableView: UITableView, indexPath: IndexPath, itemIdentifier: UUID) -> UITableViewCell? 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
Identifying items
Identifying sections
Updating data
snapshot()apply(_:animatingDifferences:)apply(_:animatingDifferences:completion:)applySnapshotUsingReloadData(_:)applySnapshotUsingReloadData(_:completion:)defaultRowAnimation