iOS13 publish
UICollectionViewDiffableDataSource
/UITableViewDiffableDataSource
, lead to a more convenient way to deal to data on collection view/table view
Create sections/items
Create UICollectionViewDiffableDataSource<Section, Item>
instance
Generic which need to provide Section/Item data
Use typealias
to shorten the type name
typealias DataSource = UICollectionViewDiffableDataSource<Section, Item>
configure data source
init(collectionView:cellProvider:)
collectionView → the collectionView which connect your data with(diffable data source)
cellProvider → where you can configure the cell content
pretty much the same behavior
collectionView(_:numberOfItemsInSection:)
collectionView(_:cellForItemAt:)
func configureDataSource() -> DataSoure {
let dataSource = DataSource(collectionView: collectionView) { collectionView, indexPath, video -> UICollectionViewCell? in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "VideoCollectionViewCell", for: indexPath) as? VideoCollectionViewCell
cell?.video = video
return cell
}
return dataSource
}
Using NSDiffableDataSourceSnapshot
Store Section/Item to display
Use typealias
to shorten the type name
typealias Snapshot = NSDiffableDataSourceSnapshot<Section, Item>
Append section/item, then apply to your dataSource
var snapshot = Snapshot()
// 要裝入sections/items
snapshot.appendSections([.main])
snapshot.appendItems(videoList)
// snapshot.appendItems(list, toSection: .main)
dataSource.apply(snapshot, animatingDifferences: animatingDifferencecs)