Created
July 19, 2019 10:26
-
-
Save AviTsadok/4db085fd04d4bcf23a2d6f380eeb1126 to your computer and use it in GitHub Desktop.
DiffableDataSource create snapshot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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