CollectionViewAdapter
open class CollectionViewAdapter<O> : BaseAdapter<O, DefaultCollectionViewTarget<UICollectionView>>, UICollectionViewDataSource where O : DynamicObject
The DiffableDataSource.CollectionViewAdapter serves as a UICollectionViewDataSource that handles ListPublisher snapshots for a UICollectionView. Subclasses of DiffableDataSource.CollectionViewAdapter may override some UICollectionViewDataSource methods as needed.
The DiffableDataSource.CollectionViewAdapter instance needs to be held on (retained) for as long as the UICollectionView‘s lifecycle.
self.dataSource = DiffableDataSource.CollectionViewAdapter<Person>(
collectionView: self.collectionView,
dataStack: CoreStoreDefaults.dataStack,
cellProvider: { (collectionView, indexPath, person) in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PersonCell") as! PersonCell
cell.setPerson(person)
return cell
}
)
The dataSource can then apply changes from a ListPublisher as shown:
listPublisher.addObserver(self) { [weak self] (listPublisher) in
self?.dataSource?.apply(
listPublisher.snapshot,
animatingDifferences: true
)
}
DiffableDataSource.CollectionViewAdapter fully handles the reload animations.
See also
CoreStore’s DiffableDataSource implementation is based on https://github.com/ra1028/DiffableDataSources-
Initializes the
DiffableDataSource.CollectionViewAdapter. This instance needs to be held on (retained) for as long as theUICollectionView‘s lifecycle.self.dataSource = DiffableDataSource.CollectionViewAdapter<Person>( collectionView: self.collectionView, dataStack: CoreStoreDefaults.dataStack, cellProvider: { (collectionView, indexPath, person) in let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PersonCell") as! PersonCell cell.setPerson(person) return cell } )Declaration
Swift
public init( collectionView: UICollectionView, dataStack: DataStack, cellProvider: @escaping (UICollectionView, IndexPath, O) -> UICollectionViewCell?, supplementaryViewProvider: @escaping (UICollectionView, String, IndexPath) -> UICollectionReusableView? = { _, _, _ in nil } )Parameters
collectionViewthe
UICollectionViewto set thedataSourceof. This instance is not retained by theDiffableDataSource.CollectionViewAdapter.dataStackthe
DataStackinstance that the dataSource will fetch objects fromcellProvidera closure that configures and returns the
UICollectionViewCellfor the objectsupplementaryViewProvideran optional closure for providing
UICollectionReusableViewsupplementary views. If not set, defaults to returningnil
View on GitHub
CollectionViewAdapter Class Reference