-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathTableViewController.swift
150 lines (115 loc) · 6.42 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// TableViewController.swift
// TableViewCellWithAutoLayout
//
// Copyright (c) 2014 Tyler Fox
//
import UIKit
class TableViewController: UITableViewController
{
let kCellIdentifier = "CellIdentifier"
var model = Model(populated: true)
override func viewDidLoad()
{
super.viewDidLoad()
title = "iOS 8 Self Sizing Cells"
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.trash, target: self, action: #selector(TableViewController.clear))
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.add, target: self, action: #selector(TableViewController.addRow))
tableView.allowsSelection = false
/******************************************************************
SWITCH BETWEEN PROGRAMMATIC AND INTERFACE BUILDER LOADED CELLS
Uncomment ONE of the two lines below to switch between approaches.
Make sure that the other line commented out - don't uncomment both!
*******************************************************************/
tableView.register(TableViewCell.self, forCellReuseIdentifier: kCellIdentifier) // uncomment this line to load table view cells programmatically
// tableView.registerNib(UINib(nibName: "NibTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kCellIdentifier) // uncomment this line to load table view cells from IB
// Self-sizing table view cells in iOS 8 require that the rowHeight property of the table view be set to the constant UITableViewAutomaticDimension
tableView.rowHeight = UITableViewAutomaticDimension
// Self-sizing table view cells in iOS 8 are enabled when the estimatedRowHeight property of the table view is set to a non-zero value.
// Setting the estimated row height prevents the table view from calling tableView:heightForRowAtIndexPath: for every row in the table on first load;
// it will only be called as cells are about to scroll onscreen. This is a major performance optimization.
tableView.estimatedRowHeight = 44.0 // set this to whatever your "average" cell height is; it doesn't need to be very accurate
}
override func viewDidAppear(_ animated: Bool)
{
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(TableViewController.contentSizeCategoryChanged(_:)), name: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil)
}
override func viewDidDisappear(_ animated: Bool)
{
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil)
}
// This function will be called when the Dynamic Type user setting changes (from the system Settings app)
func contentSizeCategoryChanged(_ notification: Notification)
{
tableView.reloadData()
}
// Deletes all rows in the table view and replaces the model with a new empty one
func clear()
{
var rowsToDelete: [IndexPath] = []
for i in 0..<model.dataArray.count {
rowsToDelete.append(IndexPath(row: i, section: 0))
}
model = Model(populated: false)
tableView.deleteRows(at: rowsToDelete, with: .automatic)
}
// Adds a single row to the table view
func addRow()
{
model.addSingleItem()
let lastIndexPath = IndexPath(row: model.dataArray.count - 1, section: 0)
tableView.insertRows(at: [lastIndexPath], with: .automatic)
}
override func numberOfSections(in tableView: UITableView) -> Int
{
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return model.dataArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
// This will be the case for programmatically loaded cells (see viewDidLoad to switch approaches)
if let cell: TableViewCell = tableView.dequeueReusableCell(withIdentifier: kCellIdentifier) as? TableViewCell {
// Configure the cell for this indexPath
cell.updateFonts()
let modelItem = model.dataArray[indexPath.row]
cell.titleLabel.text = modelItem.title
cell.bodyLabel.text = modelItem.body
// Make sure the constraints have been added to this cell, since it may have just been created from scratch
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
return cell
}
// This will be the case for interface builder loaded cells (see viewDidLoad to switch approaches)
if let cell: NibTableViewCell = tableView.dequeueReusableCell(withIdentifier: kCellIdentifier) as? NibTableViewCell {
// Configure the cell for this indexPath
cell.updateFonts()
let modelItem = model.dataArray[indexPath.row]
cell.titleLabel.text = modelItem.title
cell.bodyLabel.text = modelItem.body
// Make sure the constraints have been added to this cell, since it may have just been created from scratch
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
return cell
}
assert(false, "The dequeued table view cell was of an unknown type!")
return UITableViewCell()
}
/*
override func tableView(tableView: UITableView!, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat
{
// If you are just returning a constant value from this method, you should instead just set the table view's
// estimatedRowHeight property (in viewDidLoad or similar), which is even faster as the table view won't
// have to call this method for every row in the table view.
//
// Only implement this method if you have row heights that vary by extreme amounts and you notice the scroll indicator
// "jumping" as you scroll the table view when using a constant estimatedRowHeight. If you do implement this method,
// be sure to do as little work as possible to get a reasonably-accurate estimate.
return 44.0
}
*/
}