Skip to content

Instantly share code, notes, and snippets.

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 AviTsadok/4db085fd04d4bcf23a2d6f380eeb1126 to your computer and use it in GitHub Desktop.
Save AviTsadok/4db085fd04d4bcf23a2d6f380eeb1126 to your computer and use it in GitHub Desktop.
DiffableDataSource create snapshot
import UIKit
class ViewController: UIViewController {
var datasource : UITableViewDiffableDataSource<Section, Item>!
var items = [Item]()
var tommorowItems = [Item]()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "CustomTableViewCell")
self.datasource = UITableViewDiffableDataSource(tableView: tableView, cellProvider: { (tableView, indexPath, item) -> UITableViewCell? in
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
cell.customLabel.text = item.data
return cell
})
items.append(Item(id: "1", data: "123"))
items.append(Item(id: "2", data: "456"))
items.append(Item(id: "3", data: "789"))
tommorowItems.append(Item(id: "4", data: "123"))
tommorowItems.append(Item(id: "5", data: "456"))
tommorowItems.append(Item(id: "6", data: "789"))
let snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([Section.today, Section.tomorrow])
snapshot.appendItems(items, toSection: Section.today)
snapshot.appendItems(tommorowItems, toSection: Section.tomorrow)
datasource.apply(snapshot)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment