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
collectionView
the
UICollectionView
to set thedataSource
of. This instance is not retained by theDiffableDataSource.CollectionViewAdapter
.dataStack
the
DataStack
instance that the dataSource will fetch objects fromcellProvider
a closure that configures and returns the
UICollectionViewCell
for the objectsupplementaryViewProvider
an optional closure for providing
UICollectionReusableView
supplementary views. If not set, defaults to returningnil