Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathQuickTableViewController.swift
More file actions
Latest commit
227 lines (188 loc) · 7.44 KB
/
Copy pathQuickTableViewController.swift
File metadata and controls
227 lines (188 loc) · 7.44 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
//
// QuickTableViewController.swift
// QuickTableViewController
//
// Created by Ben on 25/08/2015.
// Copyright (c) 2015 bcylin.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import UIKit
/// A table view controller that shows `tableContents` as formatted sections and rows.
openclassQuickTableViewController:UIViewController,UITableViewDataSource,UITableViewDelegate{
/// A Boolean value indicating if the controller clears the selection when the collection view appears.
openvarclearsSelectionOnViewWillAppear=true
/// Returns the table view managed by the controller object.
openvartableView:UITableView=UITableView(frame:.zero, style:.grouped)
/// The layout of sections and rows to display in the table view.
openvartableContents:[Section]=[]{
didSet {
tableView.reloadData()
}
}
// MARK: - Initialization
/// Initializes a table view controller to manage a table view of a given style.
///
/// - Parameter style: A constant that specifies the style of table view that the controller object is to manage.
publicinit(style:UITableView.Style){
super.init(nibName:nil, bundle:nil)
tableView =UITableView(frame:.zero, style: style)
}
/// Returns a newly initialized view controller with the nib file in the specified bundle.
///
/// - Parameters:
/// - nibNameOrNil: The name of the nib file to associate with the view controller.
/// - nibBundleOrNil: The bundle in which to search for the nib file.
publicoverrideinit(nibName nibNameOrNil:String?, bundle nibBundleOrNil:Bundle?){
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
/// Returns an object initialized from data in a given unarchiver.
///
/// - Parameter coder: An unarchiver object.
publicrequiredinit?(coder:NSCoder){
super.init(coder: coder)
}
deinit{
tableView.dataSource =nil
tableView.delegate =nil
}
// MARK: - UIViewController
openoverridefunc viewDidLoad(){
super.viewDidLoad()
view.addSubview(tableView)
tableView.frame = view.bounds
tableView.autoresizingMask =[.flexibleWidth,.flexibleHeight]
tableView.rowHeight =UITableView.automaticDimension
tableView.estimatedRowHeight =44
tableView.dataSource =self
tableView.delegate =self
#if os(tvOS)
tableView.remembersLastFocusedIndexPath =true
#endif
}
openoverridefunc viewWillAppear(_ animated:Bool){
super.viewWillAppear(animated)
iflet indexPath = tableView.indexPathForSelectedRow, clearsSelectionOnViewWillAppear {
tableView.deselectRow(at: indexPath, animated:true)
}
}
// MARK: - UITableViewDataSource
openfunc numberOfSections(in tableView:UITableView)->Int{
return tableContents.count
}
openfunc tableView(_ tableView:UITableView, numberOfRowsInSection section:Int)->Int{
returntableContents[section].rows.count
}
openfunc tableView(_ tableView:UITableView, titleForHeaderInSection section:Int)->String?{
returntableContents[section].title
}
openfunc tableView(_ tableView:UITableView, cellForRowAt indexPath:IndexPath)->UITableViewCell{
letrow=tableContents[indexPath.section].rows[indexPath.row]
letcell=
tableView.dequeueReusableCell(withIdentifier: row.cellReuseIdentifier)??
row.cellType.init(style: row.cellStyle, reuseIdentifier: row.cellReuseIdentifier)
cell.defaultSetUp(with: row)
(cell as?Configurable)?.configure(with: row)
#if os(iOS)
(cell as?SwitchCell)?.delegate =self
#endif
row.customize?(cell, row)
return cell
}
openfunc tableView(_ tableView:UITableView, titleForFooterInSection section:Int)->String?{
returntableContents[section].footer
}
// MARK: - UITableViewDelegate
openfunc tableView(_ tableView:UITableView, shouldHighlightRowAt indexPath:IndexPath)->Bool{
returntableContents[indexPath.section].rows[indexPath.row].isSelectable
}
openfunc tableView(_ tableView:UITableView, didSelectRowAt indexPath:IndexPath){
letsection=tableContents[indexPath.section]
letrow= section.rows[indexPath.row]
switch(section, row){
caselet(radio as RadioSection,option as OptionRowCompatible):
letchanges:[IndexPath]= radio.toggle(option).map{
IndexPath(row: $0, section: indexPath.section)
}
if changes.isEmpty {
tableView.deselectRow(at: indexPath, animated:false)
}else{
tableView.reloadRows(at: changes, with:.automatic)
}
caselet(_,option as OptionRowCompatible):
// Allow OptionRow to be used without RadioSection.
option.isSelected = !option.isSelected
tableView.reloadData()
#if os(tvOS)
caselet(_,row as SwitchRowCompatible):
// SwitchRow on tvOS behaves like OptionRow.
row.switchValue = !row.switchValue
tableView.reloadData()
#endif
case(_, is TapActionRowCompatible):
tableView.deselectRow(at: indexPath, animated:true)
// Avoid some unwanted animation when the action also involves table view reload.
DispatchQueue.main.async{
row.action?(row)
}
caselet(_, row)where row.isSelectable:
DispatchQueue.main.async{
row.action?(row)
}
default:
break
}
}
#if os(iOS)
publicfunc tableView(_ tableView:UITableView, accessoryButtonTappedForRowWith indexPath:IndexPath){
switchtableContents[indexPath.section].rows[indexPath.row]{
caseletrow as NavigationRowCompatible:
DispatchQueue.main.async{
row.accessoryButtonAction?(row)
}
default:
break
}
}
#endif
#if os(tvOS)
privatevarcurrentFocusedIndexPath:IndexPath?
openoverridefunc didUpdateFocus(in context:UIFocusUpdateContext, with coordinator:UIFocusAnimationCoordinator){
currentFocusedIndexPath =(context.nextFocusedView as?UITableViewCell).flatMap(tableView.indexPath(for:))
}
publicfunc indexPathForPreferredFocusedView(in tableView:UITableView)->IndexPath?{
return currentFocusedIndexPath
}
#endif
}
// MARK: - SwitchCellDelegate
#if os(iOS)
extensionQuickTableViewController:SwitchCellDelegate{
@objc
openfunc switchCell(_ cell:SwitchCell, didToggleSwitch isOn:Bool){
guard
let indexPath = tableView.indexPath(for: cell),
let row =tableContents[indexPath.section].rows[indexPath.row]as?SwitchRowCompatible
else{
return
}
row.switchValue = isOn
}
}
#endif