-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathCustomTableView.swift
219 lines (182 loc) · 6 KB
/
CustomTableView.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
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
//
// CustomTableView.swift
//
// Created by Aliaksandr Dvoineu on 16.03.23.
//
import UIKit
class TableViewHeader: UIView {
lazy var view: UIView = {
let view = UIView()
return view
}()
override init(frame: CGRect) {
super.init(frame: .zero)
configure()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configure() {
addSubview(view)
view.anchors.edges.pin()
}
}
class CustomTableViewCell: UITableViewCell {
var selectionCallback: () -> () = { }
var deletionCallback: (() -> ())?
enum Action {
case toggleCheckmark
}
@discardableResult
func onSelect(callback: @escaping () -> ()) -> Self {
selectionStyle = .default
selectionCallback = callback
return self
}
@discardableResult
func onSwipeToDelete(callback: @escaping () -> ()) -> Self {
deletionCallback = callback
return self
}
@discardableResult
func onSelect(_ action: Action, callback: @escaping () -> () = { }) -> Self {
selectionCallback = { [unowned self] in
switch action {
case .toggleCheckmark:
if self.accessoryType == .checkmark {
self.accessoryType = .none
} else {
self.accessoryType = .checkmark
}
}
callback()
}
return self
}
}
final class CustomTableView: UITableView {
// Resizing UITableView to fit content
override var contentSize: CGSize {
didSet {
invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
layoutIfNeeded()
return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
}
var rows: [CustomTableViewCell] = []
var deselectsCellsAutomatically: Bool = false
var headerView = TableViewHeader()
override init(frame: CGRect, style: UITableView.Style) {
super.init(frame: frame, style: .grouped)
setup()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setup() {
dataSource = self
delegate = self
separatorStyle = .none
}
enum Insert {
case last
case dontInsert
}
@discardableResult
func addHeader(_ configure: (UIView) -> ()) -> TableViewHeader {
let header = headerView
configure(header.view)
return header
}
@discardableResult
func addRow(insert: Insert = .last, _ configure: (UIView) -> ()) -> CustomTableViewCell {
let cell = CustomTableViewCell()
cell.selectionStyle = .none
cell.backgroundColor = nil
configure(cell.contentView)
self.insert(cell: cell, insert: insert)
return cell
}
@discardableResult
func addRowCell(insert: Insert = .last, _ configure: (UITableViewCell) -> ()) -> CustomTableViewCell {
let cell = CustomTableViewCell()
cell.selectionStyle = .none
configure(cell)
self.insert(cell: cell, insert: insert)
return cell
}
@discardableResult
func addCell(insert: Insert = .last, _ cell: CustomTableViewCell) -> CustomTableViewCell {
self.insert(cell: cell, insert: insert)
return cell
}
@discardableResult
func addRow(insert: Insert = .last, view: UIView, insets: UIEdgeInsets = .zero) -> CustomTableViewCell {
return addRow(insert: insert) { (row) in
row.addSubview(view)
view.anchors.edges.pin(axis: .vertical)
view.anchors.edges.marginsPin(insets: insets, axis: .horizontal)
}
}
private func insert(cell: CustomTableViewCell, insert: Insert) {
switch insert {
case .dontInsert:
break
case .last:
rows.append(cell)
}
}
func clear() {
rows = []
}
}
extension CustomTableView: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rows.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = headerView
return header
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return rows[indexPath.row]
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
let cell = rows[indexPath.row]
return cell.deletionCallback != nil
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
let cell = rows[indexPath.row]
guard cell.deletionCallback != nil else {
return
}
cell.deletionCallback?()
self.rows.removeAll(where: { $0 === cell })
self.deleteRows(at: [indexPath], with: .fade)
}
}
extension CustomTableView: UITableViewDelegate {
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
if let cell = tableView.cellForRow(at: indexPath) as? CustomTableViewCell {
return cell.selectionStyle != .none
} else {
return false
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? CustomTableViewCell {
cell.selectionCallback()
if deselectsCellsAutomatically {
tableView.deselectRow(at: indexPath, animated: true)
}
}
}
}