This repository was archived by the owner on Jun 4, 2026. It is now read-only.
forked from venmo/Static
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSource.swift
More file actions
280 lines (217 loc) · 9.31 KB
/
Copy pathDataSource.swift
File metadata and controls
280 lines (217 loc) · 9.31 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import UIKit
/// Table view data source.
///
/// You should always access this object from the main thread since it talks to UIKit.
public class DataSource: NSObject {
// MARK: - Properties
/// The table view that will use this object as its data source.
public weak var tableView: UITableView? {
willSet {
if let tableView = tableView {
tableView.dataSource = nil
tableView.delegate = nil
}
registeredCellIdentifiers.removeAll()
}
didSet {
assert(NSThread.isMainThread(), "You must access Static.DataSource from the main thread.")
updateTableView()
}
}
/// Sections to use in the table view.
public var sections: [Section] {
didSet {
assert(NSThread.isMainThread(), "You must access Static.DataSource from the main thread.")
refresh()
}
}
/// Section index titles.
public var sectionIndexTitles: [String]? {
didSet {
assert(NSThread.isMainThread(), "You must access Static.DataSource from the main thread.")
tableView?.reloadData()
}
}
/// Automatically deselect rows after they are selected
public var automaticallyDeselectRows = true
private var registeredCellIdentifiers = Set<String>()
// MARK: - Initializers
/// Initialize with optional `tableView` and `sections`.
public init(tableView: UITableView? = nil, sections: [Section]? = nil) {
assert(NSThread.isMainThread(), "You must access Static.DataSource from the main thread.")
self.tableView = tableView
self.sections = sections ?? []
super.init()
updateTableView()
}
deinit {
// nil out the table view to ensure the table view data source and delegate niled out
tableView = nil
}
// MARK: - Public
public func rowAtPoint(point: CGPoint) -> Row? {
guard let indexPath = tableView?.indexPathForRowAtPoint(point) else { return nil }
return rowForIndexPath(indexPath)
}
// MARK: - Private
private func updateTableView() {
guard let tableView = tableView else { return }
tableView.dataSource = self
tableView.delegate = self
refresh()
}
private func refresh() {
refreshTableSections()
refreshRegisteredCells()
}
private func sectionForIndex(index: Int) -> Section? {
if sections.count <= index {
assert(false, "Invalid section index: \(index)")
return nil
}
return sections[index]
}
private func rowForIndexPath(indexPath: NSIndexPath) -> Row? {
if let section = sectionForIndex(indexPath.section) {
let rows = section.rows
if rows.count >= indexPath.row {
return rows[indexPath.row]
}
}
assert(false, "Invalid index path: \(indexPath)")
return nil
}
private func refreshTableSections(oldSections: [Section]? = nil) {
guard let tableView = tableView else { return }
guard let oldSections = oldSections else {
tableView.reloadData()
return
}
let oldCount = oldSections.count
let newCount = sections.count
let delta = newCount - oldCount
let animation: UITableViewRowAnimation = .Automatic
tableView.beginUpdates()
if delta == 0 {
tableView.reloadSections(NSIndexSet(indexesInRange: NSMakeRange(0, newCount)), withRowAnimation: animation)
} else {
if delta > 0 {
// Insert sections
tableView.insertSections(NSIndexSet(indexesInRange: NSMakeRange(oldCount - 1, delta)), withRowAnimation: animation)
} else {
// Remove sections
tableView.deleteSections(NSIndexSet(indexesInRange: NSMakeRange(oldCount - 1, -delta)), withRowAnimation: animation)
}
// Reload existing sections
let commonCount = min(oldCount, newCount)
tableView.reloadSections(NSIndexSet(indexesInRange: NSMakeRange(0, commonCount)), withRowAnimation: animation)
}
tableView.endUpdates()
}
private func refreshRegisteredCells() {
// A table view is required to manipulate registered cells
guard let tableView = tableView else { return }
// Filter to only rows with unregistered cells
let rows = sections.flatMap{ $0.rows }.filter { !self.registeredCellIdentifiers.contains($0.cellIdentifier) }
for row in rows {
let identifier = row.cellIdentifier
// Check again in case there were duplicate new cell classes
if registeredCellIdentifiers.contains(identifier) {
continue
}
registeredCellIdentifiers.insert(identifier)
if let nib = row.cellClass.nib() {
tableView.registerNib(nib, forCellReuseIdentifier: identifier)
} else {
tableView.registerClass(row.cellClass, forCellReuseIdentifier: identifier)
}
}
}
}
extension DataSource: UITableViewDataSource {
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sectionForIndex(section)?.rows.count ?? 0
}
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let row = rowForIndexPath(indexPath) {
let tableCell = tableView.dequeueReusableCellWithIdentifier(row.cellIdentifier, forIndexPath: indexPath)
if let cell = tableCell as? CellType {
cell.configure(row: row)
}
return tableCell
}
return UITableViewCell()
}
public func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sections.count
}
public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionForIndex(section)?.header?.title
}
public func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return sectionForIndex(section)?.header?.view
}
public func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return sectionForIndex(section)?.header?.viewHeight ?? UITableViewAutomaticDimension
}
public func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return sectionForIndex(section)?.footer?.title
}
public func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return sectionForIndex(section)?.footer?.view
}
public func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return sectionForIndex(section)?.footer?.viewHeight ?? UITableViewAutomaticDimension
}
public func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return rowForIndexPath(indexPath)?.canEdit ?? false
}
public func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
return rowForIndexPath(indexPath)?.editActions.map {
action in
let rowAction = UITableViewRowAction(style: action.style, title: action.title) { (_, _) in
action.selection?()
}
// These calls have side effects when setting to nil
// Setting a background color to nil will wipe out any predefined style
// Wrapping these in if-lets prevents nil-setting side effects
if let backgroundColor = action.backgroundColor {
rowAction.backgroundColor = backgroundColor
}
if let backgroundEffect = action.backgroundEffect {
rowAction.backgroundEffect = backgroundEffect
}
return rowAction
}
}
public func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
guard let sectionIndexTitles = sectionIndexTitles where sectionIndexTitles.count >= sections.count else { return nil }
return sectionIndexTitles
}
public func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
for (i, section) in sections.enumerate() {
if let indexTitle = section.indexTitle where indexTitle == title {
return i
}
}
return max(index, sections.count - 1)
}
}
extension DataSource: UITableViewDelegate {
public func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return rowForIndexPath(indexPath)?.isSelectable ?? false
}
public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if automaticallyDeselectRows {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
if let row = rowForIndexPath(indexPath) {
row.selection?()
}
}
public func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
if let row = rowForIndexPath(indexPath) {
row.accessory.selection?()
}
}
}