CNLiveHomeSearchHistoryCell.swift
5.33 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
//
// CNLiveHomeSearchHistoryCell.swift
// metaCode
//
// Created by zx on 2025/3/8.
//
import Foundation
import QMUIKit
typealias CNLiveHomeSearchHistoryClickBlock = (_ selectText:String)->Void;
typealias CNLiveHomeSearchHistoryHeightUpdateBlock = (_ height:CGFloat)->Void;
class CNLiveHomeSearchHistoryCell: UITableViewCell {
var searchHistoryClickBlock:CNLiveHomeSearchHistoryClickBlock?
var newHeight:CNLiveHomeSearchHistoryHeightUpdateBlock?
//_historyModel
var _historyModel : CNLiveHomeSearchHistoryModel?
var historyModel : CNLiveHomeSearchHistoryModel? {
get{
_historyModel
}
set{
_historyModel = newValue!
for obj in self.historyItemView.subviews{
obj.removeFromSuperview()
}
self.setupNewUI(model: _historyModel!)
}
}
lazy var titleLabel: UILabel = {
let titleLbl = UILabel()
titleLbl.font = BoldFont_16
titleLbl.textColor = UIColor(red:0.62, green:0.62, blue:0.62, alpha:1.00)
titleLbl.text = "搜索历史"
return titleLbl
}()
lazy var historyItemView : CNLiveSearchHistoryFloatLayoutView = {
let historyItemView = CNLiveSearchHistoryFloatLayoutView()
historyItemView.padding = UIEdgeInsets(top: 0, left: 0, bottom: 9, right: 0)
historyItemView.itemMargins = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 10)
historyItemView.minimumItemSize = CGSize(width: 43, height: 29)
historyItemView.zxMaxShowLinesCount = 10
return historyItemView
}()
lazy var noHisLabel: UILabel = {
let titleLbl = UILabel()
titleLbl.font = Font_14
titleLbl.textColor = UIColor(red:0.62, green:0.62, blue:0.62, alpha:1.00)
titleLbl.text = "暂无搜索历史"
return titleLbl
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = .none
contentView.backgroundColor = .white
contentView.clipsToBounds = true
self.contentView.addSubview(self.titleLabel)
titleLabel.snp.makeConstraints { make in
make.left.equalTo(15)
make.top.equalTo(20)
}
self.contentView.addSubview(self.historyItemView)
let contentWidth = KSreenWidth-30
let floatLayoutViewSize = self.historyItemView.sizeThatFits(CGSize(width: contentWidth, height: CGFLOAT_MAX))
self.historyItemView.snp.makeConstraints { make in
make.left.equalTo(15)
make.width.equalTo(KSreenWidth-30)//make.right.equalToSuperview().offset(-15)
make.top.equalTo(self.titleLabel.snp.bottom)//.offset(20)
make.height.equalTo(floatLayoutViewSize.height)
}
self.contentView.addSubview(self.noHisLabel)
self.noHisLabel.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.centerY.equalToSuperview().offset(20)
}
}
override func layoutSubviews() {
super.layoutSubviews()
if self.historyModel?.history.count ?? 0 > 0{
self.newHeight?(self.historyItemView.bottom)
}else{
self.newHeight?(176)
}
print(self.historyItemView)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupNewUI(model:CNLiveHomeSearchHistoryModel){
//倒序
let reversedArray = Array(model.history.reversed())
if reversedArray.count > 0{
var hisArr = [String]()
if reversedArray.count > 10{
hisArr = Array(reversedArray.prefix(10))
}else{
hisArr = reversedArray
}
for hisStr in hisArr {
let closeBtn = QMUIGhostButton(type: .custom)
closeBtn.ghostColor = HexColor("#999999")
closeBtn.qmui_borderColor = .clear
closeBtn.layer.borderWidth = 0
closeBtn.layer.borderColor = UIColor.clear.cgColor
closeBtn.setTitle(hisStr, for: .normal)
closeBtn.backgroundColor = HexColor("#FAFAFA")
closeBtn.titleLabel?.font = Font_14
closeBtn.contentEdgeInsets = UIEdgeInsets(top: 4, left: 10, bottom: 4, right: 10)
closeBtn.addTarget(self, action: #selector(hisBtnAction), for: .touchUpInside)
self.historyItemView.addSubview(closeBtn)
}
let contentWidth = KSreenWidth-30
let floatLayoutViewSize = self.historyItemView.sizeThatFits(CGSize(width: contentWidth, height: CGFLOAT_MAX))
self.historyItemView.snp.remakeConstraints { make in
make.left.equalTo(15)
make.width.equalTo(KSreenWidth-30)
make.top.equalTo(self.titleLabel.snp.bottom).offset(20)
make.height.equalTo(floatLayoutViewSize.height)
}
self.noHisLabel.isHidden = true
}else{
//没有历史
self.noHisLabel.isHidden = false
}
}
@objc func hisBtnAction(button:QMUIGhostButton){
//点击历史按钮
if let text = button.titleLabel?.text{
self.searchHistoryClickBlock?(text)
}
}
}