-
Notifications
You must be signed in to change notification settings - Fork 912
/
RootViewController.swift
80 lines (65 loc) · 2.22 KB
/
RootViewController.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
import UIKit
func delay(_ delay:Double, closure:@escaping ()->()) {
let when = DispatchTime.now() + delay
DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}
extension CGRect {
init(_ x:CGFloat, _ y:CGFloat, _ w:CGFloat, _ h:CGFloat) {
self.init(x:x, y:y, width:w, height:h)
}
}
extension CGSize {
init(_ width:CGFloat, _ height:CGFloat) {
self.init(width:width, height:height)
}
}
extension CGPoint {
init(_ x:CGFloat, _ y:CGFloat) {
self.init(x:x, y:y)
}
}
extension CGVector {
init (_ dx:CGFloat, _ dy:CGFloat) {
self.init(dx:dx, dy:dy)
}
}
class RootViewController: UITableViewController {
var pep : [String]! = ["Manny", "Moe", "Jack"] // pretend model might not be ready
override func viewDidLoad() {
super.viewDidLoad()
// self.refreshControl = UIRefreshControl()
// self.refreshControl!.addTarget(self, action: "doRefresh", forControlEvents: .ValueChanged)
// uncomment to show that the refresh control is hidden behind the background view
// let v = UIView()
// v.backgroundColor = .yellow
// self.tableView.backgroundView = v
}
override func numberOfSections(in tableView: UITableView) -> Int {
if self.pep == nil {
return 0
}
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.pep.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"Cell", for: indexPath)
cell.textLabel!.text = pep[indexPath.row]
return cell
}
@IBAction func doRefresh(_ sender: AnyObject) {
print("refreshing...")
delay(3) {
(sender as! UIRefreshControl).endRefreshing()
print("done")
}
}
@IBAction func doRefreshManually(_ sender: AnyObject) {
self.tableView.setContentOffset(
CGPoint(0, -self.refreshControl!.bounds.height),
animated:true)
self.refreshControl!.beginRefreshing()
self.doRefresh(self.refreshControl!)
}
}