Skip to content

Instantly share code, notes, and snippets.

@pranjalsatija
Last active February 8, 2020 00:15
Show Gist options
  • Save pranjalsatija/256d4bda2bb15d2019df415e9ed9c9f5 to your computer and use it in GitHub Desktop.
Save pranjalsatija/256d4bda2bb15d2019df415e9ed9c9f5 to your computer and use it in GitHub Desktop.
A simple example that shows how to use UITableViewDiffableDataSource.
typealias SectionType: Hashable // The type you're using to identify sections.
typealias DataType: Hashable // The type of the data you're displaying.
let dataSource = UITableViewDiffableDataSource<SectionType, DataType>(
tableView: tableView,
cellProvider: {(tableView, indexPath, data) in
// construct and return a cell here
}
)
tableView.dataSource = dataSource
tableView.delegate = self
var snapshot = NSDiffableDataSourceSnapshot<SectionType, DataType>()
snapshot.appendSections(sections) // Where sections is an array of SectionTypes that uniquely identify each section.
sections.forEach { snapshot.appendItems($0.items, toSection: $0 }
dataSource.apply(snapshot, animatingDifferences: true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment