Skip to content

Instantly share code, notes, and snippets.

@tifoaudii
Created April 5, 2020 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tifoaudii/bc5793c1d1ed872d7298539fbe32eb69 to your computer and use it in GitHub Desktop.
Save tifoaudii/bc5793c1d1ed872d7298539fbe32eb69 to your computer and use it in GitHub Desktop.
movieDataSource = UICollectionViewDiffableDataSource<MovieSection, Movie>(collectionView: movieCollectionView, cellProvider: { (collectionView, indexPath, movie) -> UICollectionViewCell? in
let movieSections = MovieSection.allCases[indexPath.section]
switch movieSections {
case .topRated:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TopRatedMovieCell.cellIdentifier, for: indexPath) as? TopRatedMovieCell else {
return TopRatedMovieCell()
}
cell.movie = movie
return cell
case .onGoing:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: OnGoingMovieCell.cellIdentifier, for: indexPath) as? OnGoingMovieCell else {
return OnGoingMovieCell()
}
cell.movie = movie
return cell
case .upcoming:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: UpcomingMovieCell.cellIdentifier, for: indexPath) as? UpcomingMovieCell else {
return UpcomingMovieCell()
}
cell.movie = movie
return cell
case .popular:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PopularMovieCell.cellIdentifier, for: indexPath) as? PopularMovieCell else {
return PopularMovieCell()
}
cell.movie = movie
return cell
}
})
movieDataSource?.supplementaryViewProvider = {
(collectionView, kind, indexPath) -> UICollectionReusableView? in
guard let supplementaryView = collectionView.dequeueReusableSupplementaryView(
ofKind: kind,
withReuseIdentifier: HeaderView.reuseIdentifier,
for: indexPath) as? HeaderView else { fatalError("Cannot create header view") }
supplementaryView.label.text = MovieSection.allCases[indexPath.section].rawValue
return supplementaryView
}
movieDataSource?.apply(generateSnapshot(), animatingDifferences: false, completion: nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment