Skip to content

Instantly share code, notes, and snippets.

@eunjin3786
Last active December 28, 2021 03:28
Show Gist options
  • Save eunjin3786/a084d37349ec57993d94c8e7ac4a31c0 to your computer and use it in GitHub Desktop.
Save eunjin3786/a084d37349ec57993d94c8e7ac4a31c0 to your computer and use it in GitHub Desktop.
import UIKit
struct Superhero: Hashable {
let name: String
}
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var dataSource: UITableViewDiffableDataSource<Int, Superhero>!
override func viewDidLoad() {
super.viewDidLoad()
// 1. Connect a diffable data source to your table view.
dataSource = UITableViewDiffableDataSource(tableView: tableView, cellProvider: { (tableView: UITableView, indexPath: IndexPath, item: Superhero) -> UITableViewCell? in
// 2. Implement a cell provider to configure your table view’s cells.
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = item.name
return cell
})
// 3. Generate the current state of the data.
var snapshot = dataSource.snapshot()
snapshot.appendSections([0])
snapshot.appendItems([Superhero(name: "스파이더맨"),
Superhero(name: "아이언맨")], toSection: 0)
// 4. Display the data in the UI.
dataSource.apply(snapshot, animatingDifferences: false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment