CNLiveShopOrderSearchViewController.swift
9.88 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
// CNLiveShopOrderSearchViewController.swift
// metaCode
//
// Created by open on 2023/11/13.
//
import Foundation
let kShopOrderListSearchHistroyDatas = "kShopOrderListSearchHistroyDatas"
class CNLiveShopOrderSearchViewController: CNLiveBaseViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
let kShopBodyOrderSearchHistoryTableViewCellIdentifier = "kShopBodyOrderSearchHistoryTableViewCellIdentifier"
lazy var tableView: UITableView = {
let tableView = UITableView.init(frame: CGRect(x: 0, y: get_system_nav_height(), width: KSreenWidth, height: KSreenHeight - get_system_nav_height()), style: .plain)
tableView.delegate = self
tableView.dataSource = self
tableView.separatorInset = UIEdgeInsets(top: 0, left: adapt_length(length: 20), bottom: 0, right: adapt_length(length: 20))
tableView.rowHeight = adapt_length(length: 45)
tableView.sectionHeaderHeight = adapt_length(length: 45)
tableView.backgroundColor = HexColor("#f1f2f6")
tableView.register(ShopBodyOrderSearchHistoryTableViewCell.self, forCellReuseIdentifier: kShopBodyOrderSearchHistoryTableViewCellIdentifier)
let footerView = UIView.init(frame: CGRectMake(0, 0, KSreenWidth, adapt_length(length: 45)))
footerView.backgroundColor = .white
let deleteBtn = UIButton.init(type: .custom)
deleteBtn.frame = footerView.bounds
if historyArr.count > 0 {
deleteBtn.setTitle("清除搜索历史", for: .normal)
deleteBtn.setTitleColor(HexColor("#FF3355"), for: .normal)
} else {
deleteBtn.setTitle("无搜索历史", for: .normal)
deleteBtn.setTitleColor(HexColor("#898989"), for: .normal)
}
deleteBtn.tag = 1
deleteBtn.titleLabel?.font = regular_adapt_font(pointSize: 14)
deleteBtn.addTarget(self, action: #selector(deleteAllBtnClick), for: .touchUpInside)
footerView.addSubview(deleteBtn)
tableView.tableFooterView = footerView
return tableView
}()
lazy var searchTextField: UITextField = {
let searchTextField = UITextField.init(frame: CGRectMake(adapt_length(length: 15), get_system_statusBar_height() + (get_system_nav_ui_height() - adapt_length(length: 30))/2, KSreenWidth - adapt_length(length: 95), adapt_length(length: 30)))
return searchTextField
}()
lazy var cancelButton: UIButton = {
let cancelButton = UIButton.init(type: .custom)
cancelButton.frame = CGRect(x: KSreenWidth - 70, y: (get_system_nav_height() - (get_system_statusBar_height() + 25))/2 + get_system_statusBar_height(), width: 60, height: 25)
cancelButton.setTitle("取消", for: .normal)
cancelButton.titleLabel?.font = semibold_adapt_font(pointSize: 15)
cancelButton.setTitleColor(HexColor("#ff3355"), for: .normal)
cancelButton.backgroundColor = .clear
cancelButton.layer.masksToBounds = true
cancelButton.layer.cornerRadius = 5
cancelButton.addTarget(self, action: #selector(cancelBtnClick), for: .touchUpInside)
return cancelButton
}()
lazy var historyArr: NSMutableArray = {
let historyArr = NSMutableArray.init()
return historyArr
}()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
take()
searchTextField.text = ""
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
tableView.frame = CGRect(x: 0, y: get_system_nav_height(), width: KSreenWidth, height: view.height - get_system_nav_height())
}
override func viewDidLoad() {
super.viewDidLoad()
navigationBarHidden = false
initNav()
view.addSubview(tableView)
}
func initNav() {
let btn = self.leftNavBtns.first! as UIButton
btn.isHidden = true
let view = UIView.init(frame: CGRectMake(0, 0, adapt_length(length: 30), adapt_length(length: 30)))
let imageView = UIImageView.init(frame: CGRect(x: adapt_length(length: 7), y: adapt_length(length: 4), width: adapt_length(length: 22), height: adapt_length(length: 22)))
imageView.image = UIImage(named: "DS_order_search")
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFit
view.addSubview(imageView)
searchTextField.leftView = view
searchTextField.delegate = self
searchTextField.leftViewMode = .always
searchTextField.placeholder = "请输入关键字..."
searchTextField.backgroundColor = HexColor("#f0f0f0")
searchTextField.returnKeyType = .search
searchTextField.layer.cornerRadius = adapt_length(length: 15)
searchTextField.layer.masksToBounds = true
searchTextField.font = regular_adapt_font(pointSize: 13)
topNavView.addSubview(searchTextField)
topNavView.addSubview(cancelButton)
}
@objc func cancelBtnClick() {
self.navigationController?.popViewController(animated: true)
}
@objc func deleteAllBtnClick(deleteBtn: UIButton) {
if historyArr.count > 0 {
deleteAll()
deleteBtn.setTitle("无搜索历史", for: .normal)
deleteBtn.setTitleColor(HexColor("#898989"), for: .normal)
}
}
///!!!:
///增删改查
func saveRecord(title: String) {
if !title.isNotBlank() {
return
}
if historyArr.contains(title) {
historyArr.remove(title)
}
historyArr.insert(title, at: 0)
tableView.reloadData()
CNLiveSaveSearchDB.shared.save(object: historyArr, key: kShopOrderListSearchHistroyDatas)
}
func take() {
let takeArr = CNLiveSaveSearchDB.shared.take(key: kShopOrderListSearchHistroyDatas)
if (takeArr as AnyObject).count > 0 {
historyArr = NSMutableArray.init(array: takeArr as! [Any])
tableView.reloadData()
}
let deleteBtn = tableView.tableFooterView?.viewWithTag(1) as! UIButton
if historyArr.count > 0 {
deleteBtn.setTitle("清除搜索历史", for: .normal)
deleteBtn.setTitleColor(HexColor("#ff3355"), for: .normal)
} else {
deleteBtn.setTitle("无搜索历史", for: .normal)
deleteBtn.setTitleColor(HexColor("#898989"), for: .normal)
}
}
func deleteAll() {
historyArr.removeAllObjects()
CNLiveSaveSearchDB.shared.delete(key: kShopOrderListSearchHistroyDatas)
tableView.reloadData()
}
func delete(index: NSInteger) {
historyArr.removeObject(at: index)
CNLiveSaveSearchDB.shared.save(object: historyArr, key: kShopOrderListSearchHistroyDatas)
tableView.reloadData()
}
}
extension CNLiveShopOrderSearchViewController {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
view.endEditing(true)
saveRecord(title: textField.text ?? "")
let delectBtn = (tableView.tableFooterView?.viewWithTag(1))! as! UIButton
delectBtn.setTitle("清除搜索历史", for: .normal)
delectBtn.setTitleColor(HexColor("#ff3355"), for: .normal)
self.navigationController?.pushViewController(CNLiveShopOrderSearchListViewController.init(searchText: textField.text!), animated: true)
return true
}
}
extension CNLiveShopOrderSearchViewController {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return historyArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: kShopBodyOrderSearchHistoryTableViewCellIdentifier) as? ShopBodyOrderSearchHistoryTableViewCell else {
return ShopBodyOrderSearchHistoryTableViewCell()
}
cell.titleLabel.text = historyArr[indexPath.row] as? String
cell.deleteBtn.tag = indexPath.row
cell.clickBlock = {[weak self] button in
self!.delete(index: button.tag)
if self!.historyArr.count == 0 {
let delectBtn = tableView.tableFooterView?.viewWithTag(1) as! UIButton
delectBtn.setTitle("无搜索历史", for: .normal)
delectBtn.setTitleColor(HexColor("#898989"), for: .normal)
}
}
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: KSreenWidth, height: adapt_length(length: 45)))
headerView.backgroundColor = .white
let titleLabel = UILabel.init(frame: CGRect(x: adapt_length(length: 20), y: 0, width: KSreenWidth - adapt_length(length: 65), height: adapt_length(length: 45)))
titleLabel.textColor = HexColor("#898989")
titleLabel.font = regular_adapt_font(pointSize: 14)
titleLabel.text = "历史搜索"
headerView.addSubview(titleLabel)
let lineLabel = UILabel.init(frame: CGRect(x: adapt_length(length: 20), y: adapt_length(length: 44), width: KSreenWidth, height: adapt_length(length: 1)))
lineLabel.backgroundColor = HexColor("#e4e4e4")
headerView.addSubview(lineLabel)
return headerView
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.navigationController?.pushViewController(CNLiveShopOrderSearchListViewController.init(searchText: historyArr[indexPath.row] as! String), animated: true)
saveRecord(title: historyArr[indexPath.row] as! String)
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
view.endEditing(true)
}
}