-
Notifications
You must be signed in to change notification settings - Fork 1
/
TableViewController.swift
90 lines (70 loc) · 3.35 KB
/
TableViewController.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
//
// Created by Nick Snyder on 5/29/15.
// Copyright (c) 2015 Example. All rights reserved.
//
import UIKit
class TableViewController: UITableViewController {
static let oneLine = "one one one one one one one"
static let twoLines = "two two two two two two two two two two two two two two"
static let threeLines = "three three three three three three three three three three three three three three three"
static let twoLinesIpad = "two ipad two ipad two ipad two ipad two ipad two ipad two ipad two ipad two ipad two ipad two ipad two ipad two ipad two ipad two ipad two ipad two ipad"
static let threeLinesIpad = "three ipad three ipad three ipad three ipad three ipad three ipad three ipad three ipad three ipad three ipad three ipad three ipad three ipad three ipad three ipad three ipad three ipad"
let contents = [
// iPhone tests
Content(title: oneLine, body: oneLine),
Content(title: oneLine, body: twoLines),
Content(title: oneLine, body: threeLines),
Content(title: twoLines, body: oneLine),
Content(title: twoLines, body: twoLines),
Content(title: twoLines, body: threeLines),
Content(title: threeLines, body: oneLine),
Content(title: threeLines, body: twoLines),
Content(title: threeLines, body: threeLines),
// iPad tests
Content(title: oneLine, body: oneLine),
Content(title: oneLine, body: twoLinesIpad),
Content(title: oneLine, body: threeLinesIpad),
Content(title: twoLinesIpad, body: oneLine),
Content(title: twoLinesIpad, body: twoLinesIpad),
Content(title: twoLinesIpad, body: threeLinesIpad),
Content(title: threeLinesIpad, body: oneLine),
Content(title: threeLinesIpad, body: twoLinesIpad),
Content(title: threeLinesIpad, body: threeLinesIpad),
]
override func viewDidLoad() {
super.viewDidLoad()
ContentTableCell.registerForTableView(tableView)
tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
// It is necessary to provide an estimated row height for automatic table cell sizing to work in iOS 8+
tableView.estimatedRowHeight = 100
}
// MARK - UITableViewDataSource
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contents.count;
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
NSLog("cellForRowAtIndexPath \(indexPath.item)")
let content = contents[indexPath.item]
let cell = ContentTableCell.dequeueForTableView(tableView, indexPath: indexPath)
cell.bind(content)
if (UIDevice.hasMajorSystemVersion(8)) {
// There is a bug in iOS 8 automatic cell height that causes layout to be incorrect in certain cases.
// This bug is fixed in iOS 9 (as of WWDC 2015 Xcode 7 beta build 7a120f).
cell.layoutIfNeeded()
}
return cell
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if (UIDevice.hasMinimumSystemVersion("8.0")) {
return UITableViewAutomaticDimension
}
let content = contents[indexPath.item]
let height = ContentTableCell.sharedCell.bind(content).heightInTableView(tableView)
NSLog("heightForRowAtIndexPath \(indexPath.item) height \(height)")
return height
}
}
struct Content {
let title: String
let body: String
}