Collection view and diffable datasource

Benefit

  1. Reduce code
  2. Automatic animation
  3. Data Synchronization
    ➡️ You have to manually handle the data change between collection view and datasource when use UICollectionViewDataSource

How to create UICollectionView using UICollectionViewDiffableDataSource

  1. Create your section/item data
  • Enum is a great way to create sections data
  • Generic which need to provide Section/Item data
  • Use typealias to shorten the type name
  • collectionView → the collectionView which connect your data with(diffable data source)
  • cellProvider → where you can configure the cell content
  • Store section/item to display
  • Use typealias to shorten the type name
  • Append section/item, then apply to your dataSource
only one section([.main]), so items videoList will all put in section .main

Demo

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store