/
MasterViewController.swift
110 lines (85 loc) · 3.23 KB
/
MasterViewController.swift
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import UIKit
func delay(_ delay:Double, closure:@escaping ()->()) {
let when = DispatchTime.now() + delay
DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}
class MyCell : UITableViewCell {
@IBOutlet weak var iv : UIImageView!
}
class MasterViewController : UITableViewController {
struct CellModel {
let name = "poppy" // hard-coded; well it's only an example
var visible = true
}
var model = [CellModel()] // only one row; I _said_ it's only an example
var lastSelection = IndexPath()
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.model.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MyCell
let cm = self.model[indexPath.row]
cell.iv.image = UIImage(named:cm.name)
if !cm.visible {
cell.iv.image = nil
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.model[indexPath.row].visible = false
tableView.reloadRows(at: [indexPath], with: .none)
self.lastSelection = indexPath
self.performSegue(withIdentifier: "showDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print(self.tableView.indexPathForSelectedRow as Any)
if let dest = segue.destination as? DetailViewController {
let cm = self.model[self.lastSelection.row]
dest.detailItem = UIImage(named:cm.name)
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
func reset(_ cm : inout CellModel) {
cm.visible = true
}
for ix in self.model.indices {reset(&self.model[ix])}
self.tableView.reloadData()
// rest is just interesting logging
print("\(self) " + #function)
if let tc = self.transitionCoordinator {
print(tc)
}
// to see this, turn off our animation and use the pop gesture
let tc = self.transitionCoordinator
tc?.notifyWhenInteractionChanges { ctx in
if ctx.isCancelled {
print("we got cancelled")
}
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("\(self) " + #function)
if let tc = self.transitionCoordinator {
print(tc)
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("\(self) " + #function)
if let tc = self.transitionCoordinator {
print(tc)
}
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
print("\(self) " + #function)
if let tc = self.transitionCoordinator {
print(tc)
}
}
}