Last active
December 28, 2021 03:28
-
-
Save eunjin3786/a084d37349ec57993d94c8e7ac4a31c0 to your computer and use it in GitHub Desktop.
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 | |
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