-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathTopViewController.swift
More file actions
82 lines (64 loc) 路 2.17 KB
/
Copy pathTopViewController.swift
File metadata and controls
82 lines (64 loc) 路 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import UIKit
import DiffableDataSources
final class TopViewController: UIViewController {
enum Section {
case main
}
enum Item {
case mountains
case insertionSort
}
@IBOutlet private var tableView: UITableView!
private lazy var dataSource = TableViewDiffableDataSource<Section, Item>(tableView: tableView) { tableView, indexPath, item in
let cell = tableView.dequeueReusableCell(withIdentifier: UITableViewCell.name, for: indexPath)
cell.textLabel?.text = item.title
cell.accessoryType = .disclosureIndicator
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
title = "DiffableDataSources"
tableView.delegate = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: UITableViewCell.name)
tableView.rowHeight = 60
tableView.contentInset.top = 30
reset()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let indexPath = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: indexPath, animated: true)
}
}
func reset() {
var snapshot = DiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.main])
snapshot.appendItems([.mountains, .insertionSort])
dataSource.apply(snapshot)
}
}
extension TopViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let item = dataSource.itemIdentifier(for: indexPath) else {
return
}
let viewController: UIViewController
switch item {
case .mountains:
viewController = MountainsViewController()
case .insertionSort:
viewController = InsertionSortViewController()
}
navigationController?.pushViewController(viewController, animated: true)
}
}
private extension TopViewController.Item {
var title: String {
switch self {
case .mountains:
return "馃椈 Mountains"
case .insertionSort:
return "馃摫 Insertion Sort"
}
}
}