-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLKCompareTableCell.swift
298 lines (243 loc) · 9.5 KB
/
LKCompareTableCell.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
// LKCompareTableCell.swift
// LKCompareTable
//
// Created by Fan Li Lin on 2022/9/21.
//
import UIKit
import SnapKit
class LKCompareTableCell: UITableViewCell {
// MARK: - Public(Ivars)
typealias indexBlock = (_ index: Int, _ itemCell: LKCompareTableItemCell) -> Void
/// scrollView 的滚动事件暴露给外层去处理
public weak var scrollViewDelegate: UIScrollViewDelegate? {
didSet {
mainScrollView.delegate = scrollViewDelegate
}
}
public var contentOffset: CGPoint = .zero
/// 用于占位补充显示数量;如: displayCount = 5,纵向列数实际数据只有3列,会补充2列空白显示
public var displayCount: Int = 0
/// 左边的字段栏
public private(set) var fieldView: LKCompareTableFieldView?
/// 左边的字段栏视图
public private(set) lazy var fieldContentView: UIView = {
let attrNameView = UIView()
return attrNameView
}()
/// 侧边滚动父视图
public private(set) lazy var mainScrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.tag = 985
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
scrollView.bounces = false
return scrollView
}()
/// 侧边自适应内容父视图
public private(set) lazy var contentStackView: LKCompareTableStackView = {
let stackView = LKCompareTableStackView()
stackView.axis = .horizontal
stackView.spacing = separatorWidth
stackView.separatorColor = separatorColor
stackView.alignment = .fill
stackView.distribution = .fill
return stackView
}()
/// 顶部分割线
public private(set) lazy var topLineView: UIView = {
let view = UIView()
view.backgroundColor = separatorColor
return view
}()
/// 底部分割线
public private(set) lazy var bottomLineView: UIView = {
let view = UIView()
view.backgroundColor = separatorColor
return view
}()
/// 右边分割线
public private(set) lazy var rightLineView: UIView = {
let view = UIView()
view.backgroundColor = separatorColor
return view
}()
// MARK: - Public(Method)
/// 返回对应的ItemCell
/// - Parameter index: 第几个
/// - Returns: LKCompareTableItemCell
public func cellForItem(at index: Int) -> LKCompareTableItemCell? {
guard contentStackView.arrangedSubviews.count > index else {
return nil
}
return contentStackView.arrangedSubviews[index] as? LKCompareTableItemCell;
}
/// 设置自定义的cell
/// - Parameters:
/// - number: 属性数量
/// - closure: 回调
public func setItemCell(with number: Int, itemCellWidth width: CGFloat,_ closure: (_ index: Int) -> UIView?) {
if number != contentStackView.arrangedSubviews.count {
for cell in contentStackView.arrangedSubviews {
cell.removeFromSuperview()
}
}
for index in 0..<number {
guard let cell = closure(index) else {
break
}
if width != LKCompareTableView.automaticDimension {
cell.snp.updateConstraints { make in
make.width.equalTo(width)
}
}
let view = contentStackView.arrangedSubviews.count > index ? contentStackView.arrangedSubviews[index] : nil;
if (view == nil) {
contentStackView.addArrangedSubview(cell)
if cell.isKind(of: LKCompareTableItemCell.self) {
cell.isUserInteractionEnabled = true
cell.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:))))
}
} else if (view != cell) {
/// 移除视图并更新的视图位置
view!.removeFromSuperview()
contentStackView.insertArrangedSubview(cell, at: index)
if cell.isKind(of: LKCompareTableItemCell.self) {
cell.isUserInteractionEnabled = true
cell.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:))))
}
}
cell.tag = index
}
/// 如果第一次contentSize为空情况下 在外部设置 contentOffset 之后会反弹
if mainScrollView.contentSize.equalTo(.zero) {
var size = contentStackView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
if size.equalTo(.zero) {
size = contentStackView.sizeThatFits(CGSize(width: frame.width, height: 0))
}
mainScrollView.contentSize = size
}
}
/// 设置自定义FieldView
/// - Parameters:
/// - width: 宽度
/// - view: FieldView
public func setFieldView(with width: CGFloat, field view: LKCompareTableFieldView) {
if fieldView != nil && view.reuseIdentifier != fieldView?.reuseIdentifier {
fieldView?.removeFromSuperview()
}
fieldView = view
if view.superview != fieldContentView {
fieldContentView.addSubview(view)
view.snp.makeConstraints { make in
make.edges.equalToSuperview()
if width != LKCompareTableView.automaticDimension {
make.width.equalTo(width)
}
}
} else {
view.snp.updateConstraints { make in
if width != LKCompareTableView.automaticDimension {
make.width.equalTo(width)
}
}
}
}
public func didSelectIndexBlock(block: @escaping indexBlock) {
didSelectIndexCall = block
}
// MARK: - Init
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
selectionStyle = .none
backgroundView?.backgroundColor = .clear
backgroundColor = .clear
contentView.backgroundColor = .white
addSubviews()
layoutPageSubviews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Lifecycle
// MARK: - Config
/// addView
private func addSubviews() {
contentView.addSubview(fieldContentView)
contentView.addSubview(mainScrollView)
mainScrollView.addSubview(contentStackView)
contentView.addSubview(topLineView)
contentView.addSubview(bottomLineView)
contentView.addSubview(rightLineView)
}
/// 布局子视图
private func layoutPageSubviews() {
fieldContentView.snp.makeConstraints { make in
make.left.bottom.top.equalToSuperview()
}
mainScrollView.snp.makeConstraints { make in
make.right.top.bottom.equalToSuperview()
make.left.equalTo(fieldContentView.snp.right)
make.height.equalTo(contentStackView);
}
contentStackView.snp.makeConstraints { make in
make.left.equalTo(mainScrollView)
make.right.equalTo(mainScrollView)
make.top.bottom.equalTo(mainScrollView)
}
topLineView.snp.makeConstraints { make in
make.left.right.top.equalToSuperview()
make.height.equalTo(separatorWidth)
}
bottomLineView.snp.makeConstraints { make in
make.left.right.bottom.equalToSuperview()
make.height.equalTo(separatorWidth)
}
rightLineView.snp.makeConstraints { make in
make.top.bottom.equalToSuperview()
make.right.equalTo(mainScrollView.snp.left)
make.width.equalTo(separatorWidth)
}
}
// MARK: - Update view
// MARK: - Action
@objc private func handleTapGesture(_ sender: UITapGestureRecognizer) {
didSelectIndexCall?(sender.view?.tag ?? 0, sender.view as! LKCompareTableItemCell)
}
// MARK: - Private(Ivars)
/// 分割线颜色
private let separatorColor = UIColor(red: 0.93, green: 0.93, blue: 0.93, alpha: 1)
/// 分割线宽度
private let separatorWidth = 1.0 / UIScreen.main.scale
private var didSelectIndexCall: indexBlock?
// MARK: - Private(Method)
}
class LKCompareTableAttrNameView: UIView {
public private(set) lazy var textLabel: UILabel = {
let label = UILabel()
label.textColor = UIColor(red: 0.4, green: 0.4, blue: 0.4, alpha: 1)
label.font = UIFont.systemFont(ofSize: 12, weight: .regular)
label.numberOfLines = 0
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubviews()
layoutPageSubviews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addSubviews() {
addSubview(textLabel)
}
func layoutPageSubviews() {
textLabel.snp.makeConstraints { make in
make.centerY.equalToSuperview()
make.left.equalTo(10)
make.right.lessThanOrEqualTo(-10)
make.top.lessThanOrEqualTo(9)
make.bottom.lessThanOrEqualTo(-9)
}
}
}