-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample04ViewController.swift
347 lines (257 loc) · 13.9 KB
/
Example04ViewController.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//
// Example04ViewController.swift
// LKCompareTable IOS Example
//
// Created by Fan Li Lin on 2022/10/7.
//
import UIKit
import SnapKit
import LKCompareTable
class Example04ViewController: UIViewController {
// MARK: - ***** Public method *****
// MARK: - ***** Ivars *****
lazy var compareTableView: LKCompareTableView = {
let tableView = LKCompareTableView(frame: .zero, headerStyle: .follow)
tableView.register(LKCompareTableItemTest01Cell.self, forCellReuseIdentifier: "LKCompareTableItemTest01Cell")
tableView.dataSourcePlug = LKCompareTableMorePlug(delegate: self)
tableView.delegate = self
tableView.headerDataSource = self
tableView.displayCount = 5
return tableView
}()
var flag: Bool = false
/// 测试数据
let testData: [LKCompareTableMoreModel] = {
var data = [LKCompareTableMoreModel]()
let attrNames1 = ["商品名称"]
let attrNames2 = ["商品分类商品分类商品分类商品分类", "商品价格", "品牌", "商品属性", "仓储地", "运距", "物流服务", "账期", "发票", "期货模式"];
let attrNames3 = ["企业名称", "客户身份", "信用评分", "主营业务", "企业类型", "经营地址", "注册资本"];
let model01 = LKCompareTableMoreModel()
/// 添加属性测试数据
model01.makeFieldModel(with: attrNames1.count) { index, attr in
attr.fieldName = attrNames1[index]
attr.contentAlignment = .centerLeft
/// 添加item测试数据
attr.makeItemModel(with: 8) { itemIndex, item in
if itemIndex == 0 { item.text = "开始-2022厂家直供钢筋 /八钢/热轧带肋钢2022厂家直供钢筋 /八钢/热轧带肋钢2022厂家直供钢筋 /八钢/热轧带肋钢结尾"}
if itemIndex == 1 { item.text = "开始-2022厂家直供钢筋 /八钢/热轧带肋钢2022厂家直供钢筋 /八钢/热轧带肋钢2022厂家直供钢筋 /八钢/热轧带肋钢结尾"}
if itemIndex == 2 { item.text = "开始-2022厂家直供钢筋 /八钢/热轧带肋钢2022厂家直供钢筋 /八钢/热轧带肋钢2022厂家直供钢筋 /八钢/热轧带肋钢结尾"}
if itemIndex == 3 { item.text = "/"}
}
}
data.append(model01)
let model02 = LKCompareTableMoreModel()
model02.headerTitle = "商品属性"
/// 添加属性测试数据
model02.makeFieldModel(with: attrNames2.count) { index, attr in
attr.fieldName = attrNames2[index]
attr.contentAlignment = .centerLeft
/// 添加item测试数据
var num: Int = Int(arc4random_uniform(8))
var values = [String]()
attr.makeItemModel(with: 8) { itemIndex, item in
if attr.fieldName == "商品价格" {
// item.text = "¥3400.00/吨"
let price = "¥3400.00/吨"
let suffix = "详情>"
let lineBreaks = "\n"
let string = price + lineBreaks + suffix
let priceAttributes = [.foregroundColor: UIColor(red: 0.94, green: 0.25, blue: 0.2, alpha: 1), .font: UIFont.systemFont(ofSize: 12, weight: .regular)] as [NSAttributedString.Key : Any]
let suffixAttributes = [.foregroundColor: UIColor(red: 0.34, green: 0.57, blue: 0.99, alpha: 1), .font: UIFont.systemFont(ofSize: 12, weight: .regular)] as [NSAttributedString.Key : Any]
let attString = NSMutableAttributedString(string: string)
attString.addAttributes(priceAttributes, range: NSRange(location: 0, length: price.count))
attString.addAttributes(suffixAttributes, range: NSRange(location: price.count + lineBreaks.count, length: suffix.count))
item.attributedText = attString
} else {
var val = "第2组下的第\(index)行的第\(itemIndex)个数据"
/// 加长一点测试
if num == itemIndex {
val += "加长的占位符加长的占位符加长的占位符加长的占位符加长的占位符加长的占位符"
}
item.text = val
}
if index == 2 && itemIndex == 3 { item.text = "/"}
}
/// 指定某一项字段换行数
if attr.fieldName == "物流服务" {
attr.numberOfLines = 3
}
}
data.append(model02)
let model03 = LKCompareTableMoreModel()
model03.headerTitle = "供应商详情"
/// 添加属性测试数据
model03.makeFieldModel(with: attrNames3.count) { index, attr in
attr.fieldName = attrNames3[index]
attr.contentAlignment = .centerLeft
/// 添加item测试数据
var num: Int = Int(arc4random_uniform(8))
var values = [String]()
attr.makeItemModel(with: 8) { itemIndex, item in
var val = "第3组下的第\(index)行的第\(itemIndex)个数据"
/// 加长一点测试
if num == itemIndex {
val += "加长的占位符加长的占位符加长的占位符加长的占位符加长的占位符加长的占位符"
}
item.text = val
}
}
data.append(model03)
return data
}()
/// 测试数据
let test2Data: [LKCompareTableMoreModel] = {
var data = [LKCompareTableMoreModel]()
let attrNames1 = ["商品名称"]
let attrNames2 = ["商品分类商品分类商品分类商品分类", "商品价格", "品牌", "商品属性", "仓储地", "运距", "物流服务", "账期", "发票", "期货模式"];
let attrNames3 = ["企业名称", "客户身份", "信用评分", "主营业务", "企业类型", "经营地址", "注册资本"];
let model01 = LKCompareTableMoreModel()
/// 添加属性测试数据
model01.makeFieldModel(with: attrNames1.count) { index, attr in
attr.fieldName = attrNames1[index]
attr.contentAlignment = .centerLeft
/// 添加item测试数据
attr.makeItemModel(with: 2) { itemIndex, item in
if itemIndex == 0 { item.text = "开始-2022厂家直供钢筋 /八钢/热轧带肋钢2022厂家直供钢筋 /八钢/热轧带肋钢2022厂家直供钢筋 /八钢/热轧带肋钢结尾"}
if itemIndex == 1 { item.text = "开始-2022厂家直供钢筋 /八钢/热轧带肋钢2022厂家直供钢筋 /八钢/热轧带肋钢2022厂家直供钢筋 /八钢/热轧带肋钢结尾"}
if itemIndex == 2 { item.text = "开始-2022厂家直供钢筋 /八钢/热轧带肋钢2022厂家直供钢筋 /八钢/热轧带肋钢2022厂家直供钢筋 /八钢/热轧带肋钢结尾"}
if itemIndex == 3 { item.text = "/"}
}
}
data.append(model01)
let model02 = LKCompareTableMoreModel()
model02.headerTitle = "商品属性"
/// 添加属性测试数据
model02.makeFieldModel(with: attrNames2.count) { index, attr in
attr.fieldName = attrNames2[index]
attr.contentAlignment = .centerLeft
/// 添加item测试数据
var num: Int = Int(arc4random_uniform(2))
var values = [String]()
attr.makeItemModel(with: 2) { itemIndex, item in
if attr.fieldName == "商品价格" {
// item.text = "¥3400.00/吨"
let price = "¥3400.00/吨"
let suffix = "详情>"
let lineBreaks = "\n"
let string = price + lineBreaks + suffix
let priceAttributes = [.foregroundColor: UIColor(red: 0.94, green: 0.25, blue: 0.2, alpha: 1), .font: UIFont.systemFont(ofSize: 12, weight: .regular)] as [NSAttributedString.Key : Any]
let suffixAttributes = [.foregroundColor: UIColor(red: 0.34, green: 0.57, blue: 0.99, alpha: 1), .font: UIFont.systemFont(ofSize: 12, weight: .regular)] as [NSAttributedString.Key : Any]
let attString = NSMutableAttributedString(string: string)
attString.addAttributes(priceAttributes, range: NSRange(location: 0, length: price.count))
attString.addAttributes(suffixAttributes, range: NSRange(location: price.count + lineBreaks.count, length: suffix.count))
item.attributedText = attString
} else {
var val = "第2组下的第\(index)行的第\(itemIndex)个数据"
/// 加长一点测试
if num == itemIndex {
val += "加长的占位符加长的占位符加长的占位符加长的占位符加长的占位符加长的占位符"
}
item.text = val
}
if index == 2 && itemIndex == 3 { item.text = "/"}
}
/// 指定某一项字段换行数
if attr.fieldName == "物流服务" {
attr.numberOfLines = 3
}
}
data.append(model02)
let model03 = LKCompareTableMoreModel()
model03.headerTitle = "供应商详情"
/// 添加属性测试数据
model03.makeFieldModel(with: attrNames3.count) { index, attr in
attr.fieldName = attrNames3[index]
attr.contentAlignment = .centerLeft
/// 添加item测试数据
var num: Int = Int(arc4random_uniform(2))
var values = [String]()
attr.makeItemModel(with: 2) { itemIndex, item in
var val = "第3组下的第\(index)行的第\(itemIndex)个数据"
/// 加长一点测试
if num == itemIndex {
val += "加长的占位符加长的占位符加长的占位符加长的占位符加长的占位符加长的占位符"
}
item.text = val
}
}
data.append(model03)
return data
}()
// MARK: - ***** Class method *****
// MARK: - ***** Init method *****
// MARK: - ***** Lifecycle *****
open override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "测试增删", style: .plain, target: self, action: #selector(rightBarButtonEvent))
addSubviews()
layoutPageSubviews()
/// next
compareTableView.datas = test2Data
}
func addSubviews() {
view.addSubview(compareTableView)
}
func layoutPageSubviews() {
compareTableView.snp.makeConstraints { make in
make.edges.equalTo(view)
}
}
// MARK: - ***** Update view *****
// MARK: - ***** Private method *****
@objc func rightBarButtonEvent() {
flag = !flag
if flag {
/// next
compareTableView.datas = testData
} else {
/// next
compareTableView.datas = test2Data
}
}
// MARK: - ***** Respond event method *****
// MARK: - ***** Create method *****
}
extension Example04ViewController: LKCompareTableViewHeaderDataSource {
func viewForHeader(in compareTableView: LKCompareTableView) -> UIView? {
let contentView = UIView()
contentView.backgroundColor = UIColor(red: 1, green: 0.97, blue: 0.9, alpha: 1)
contentView.snp.makeConstraints { make in
make.height.equalTo(40)
}
let icon = UIImageView()
icon.image = UIImage(named: "zhuyi")
contentView.addSubview(icon)
let label = UILabel()
label.text = "数据仅供参考,具体以供应商接单为准"
label.font = UIFont.systemFont(ofSize: 13, weight: .regular)
label.textColor = UIColor(red: 1, green: 0.75, blue: 0, alpha: 1)
contentView.addSubview(label)
icon.snp.makeConstraints { make in
make.left.equalTo(10)
make.centerY.equalToSuperview()
}
label.snp.makeConstraints { make in
make.left.equalTo(icon.snp.right).offset(5)
make.centerY.equalToSuperview()
}
return contentView
}
}
extension Example04ViewController: LKCompareTableViewDelegate {
func compareTableView(_ compareTableView: LKCompareTableView, didSelectRowAt indexPath: IndexPath, to index: Int) {
print("点击了第\(indexPath.section)组下的第\(indexPath.row)行的第\(index)个")
}
}
extension Example04ViewController: LKCompareTableMorePlugDelegate {
func compareTableView(_ compareTableView: LKCompareTableView, didSelectRowAt indexPath: IndexPath, to index: Int, is truncated: Bool, deal result: Any?) {
if truncated {
let alert = UIAlertController(title: "提示", message: "需要查看更多", preferredStyle: .alert)
let good = UIAlertAction(title: "好的", style: .default)
alert.addAction(good)
present(alert, animated: true)
print("需要查看更多")
}
}
}