Why


iOS13 publish UICollectionViewDiffableDataSource/UITableViewDiffableDataSource, lead to a more convenient way to deal to data on collection view/table view

Benefit


  1. Reduce code
  2. Automatic animation
  3. Data Synchronization → You have to manually handle the data change between collection/table view and datasource

How to use


  1. Create sections/items

  2. Create UICollectionViewDiffableDataSource<Section, Item> instance

  3. configure data source

    1. 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
    }
    
  4. Using NSDiffableDataSourceSnapshot

Alert


Demo


collectionview.gif