CNLiveHomeSearchController.swift
30 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
//
// CNLiveHomeSearchController.swift
// metaCode
//
// Created by zx on 2025/3/7.
//
//
import Foundation
import QMUIKit
import MJRefresh
class CNLiveHomeSearchController:CNLiveBaseViewController,UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate{
let CNLiveHomeSearchHistoryKey = "CNLiveHomeSearchHistoryKey"
lazy var searBarView :QMUISearchBar = {
let iconImgView = QMUISearchBar(frame: CGRect(x: 15, y: KStatusBarHeight, width: KSreenWidth-50-15, height: 32))
iconImgView.backgroundColor = .white//.clear
iconImgView.searchTextField.backgroundColor = .white
iconImgView.searchTextField.clearButtonMode = .whileEditing
iconImgView.searchTextField.font = Font_13
iconImgView.searchTextField.textColor = HexColor("#333333")
iconImgView.clipsToBounds = true
iconImgView.layer.cornerRadius = 16
iconImgView.backgroundImage = UIImage(named: "home_searchBg")
iconImgView.scopeBarBackgroundImage = UIImage(named: "home_searchBg")
iconImgView.contentMode = .scaleAspectFill
iconImgView.backgroundColor = .clear
iconImgView.searchTextField.backgroundColor = .clear
iconImgView.inputView?.backgroundColor = .clear
iconImgView.delegate = self
return iconImgView
}()
lazy var searchBgImgView :UIImageView = {
let iconImgView = UIImageView()
iconImgView.image = UIImage(named: "home_searchBg")
iconImgView.contentMode = .scaleAspectFill
return iconImgView
}()
lazy var cancelBtn: QMUIButton = {
let playBtn = QMUIButton.init(type: .custom)
playBtn.setTitle("取消", for: .normal)
playBtn.setTitleColor(HexColor("#999999"), for: .normal)
playBtn.titleLabel?.font = Font_15
playBtn.addTarget(self, action: #selector(cancelBtnAction), for: .touchUpInside)
return playBtn
}()
///数据源
lazy var dataArray: NSMutableArray = {
var dataArray = NSMutableArray()
return dataArray
}()
let kCNLiveHomeSearchHotCellIdentifier = "kCNLiveHomeSearchHotCellIdentifier"
let kCNLiveHomeSearchHistoryCellIdentifier = "kCNLiveHomeSearchHistoryCellIdentifier"
lazy var tableView : UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.dataSource = self
tableView.delegate = self
tableView.separatorStyle = .none
tableView.register(CNLiveHomeSearchHistoryCell.self, forCellReuseIdentifier: kCNLiveHomeSearchHistoryCellIdentifier)
tableView.register(CNLiveHomeSearchHotCell.self, forCellReuseIdentifier: kCNLiveHomeSearchHotCellIdentifier)
tableView.backgroundColor = .white
return tableView
}()
var searchText = ""//搜索内容
var hisArr = [String]()//搜索本地历史
var page = 1//当前页数
var historyHeight:CGFloat = 176//默认高度
// MARK: - 搜索结果
//搜索结果数组
lazy var resultListArray: NSMutableArray = {
var dataArray = NSMutableArray()
return dataArray
}()
let kCNLiveHomeSearchResultJuJiCellIdentifier = "kCNLiveHomeSearchResultJuJiCellIdentifier"
// let kCNLiveHomeSearchResultDuanJuCellIdentifier = "kCNLiveHomeSearchResultDuanJuCellIdentifier"
lazy var resultTableView : UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.dataSource = self
tableView.delegate = self
tableView.separatorStyle = .none
tableView.register(CNLiveHomeSearchResultJuJiCell.self, forCellReuseIdentifier: kCNLiveHomeSearchResultJuJiCellIdentifier)
// tableView.register(CNLiveHomeSearchResultDuanJuCell.self, forCellReuseIdentifier: kCNLiveHomeSearchResultDuanJuCellIdentifier)
tableView.backgroundColor = .white
return tableView
}()
var resultHeaderView:UIView?
var footerRefreshView: MJRefreshAutoNormalFooter!
var isJujiModel:Bool = false//搜索结果 默认单集
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
view.addSubview(searBarView)
searBarView.snp.makeConstraints { make in
make.left.equalToSuperview().offset(15)
make.top.equalToSuperview().offset(KStatusBarHeight)
make.width.equalTo((KSreenWidth-50-15 > 310) ? 310 : KSreenWidth-50-15 )
make.height.equalTo(32)
}
// view.addSubview(searchBgImgView)
// searchBgImgView.snp.makeConstraints { make in
// make.left.equalToSuperview().offset(15)
// make.top.equalToSuperview().offset(KStatusBarHeight)
// make.width.equalTo(KSreenWidth-50-15)
// make.height.equalTo(32)
// }
view.addSubview(cancelBtn)
cancelBtn.snp.makeConstraints { make in
make.left.equalTo(self.searBarView.snp.right)
make.top.equalTo(self.searBarView.snp.top)
make.bottom.equalTo(self.searBarView.snp.bottom)
make.right.equalToSuperview()
}
view.addSubview(tableView)
tableView.snp.makeConstraints { make in
make.left.equalToSuperview()
make.top.equalTo(self.searBarView.snp.bottom)
make.bottom.equalToSuperview()
make.right.equalToSuperview()
}
self.recordSearchHistory()
self.getSearchHotListApi()
view.addSubview(resultTableView)
resultTableView.isHidden = true
resultTableView.snp.makeConstraints { make in
make.left.equalToSuperview()
make.top.equalTo(self.searBarView.snp.bottom)
make.bottom.equalToSuperview()
make.right.equalToSuperview()
}
self.setUpResultHeaderView()
self.addRefreshView()
}
lazy var headerJuJiBtn: UIButton = {
let expandBtn = UIButton.init(type: .custom)
expandBtn.backgroundColor = .clear
expandBtn.setTitleColor(HexColor("#919191"), for: .normal)
expandBtn.titleLabel?.font = BoldFont_13
expandBtn.setTitle("电视剧", for: .normal)
expandBtn.layer.cornerRadius = 16
expandBtn.clipsToBounds = true
expandBtn.frame = CGRect(x: 0, y: 0, width: 60, height: 32)
expandBtn.addTarget(self, action: #selector(headerJuJiBtnAction), for: .touchUpInside)
return expandBtn
}()
lazy var headerDuanJuBtn: UIButton = {
let expandBtn = UIButton.init(type: .custom)
expandBtn.backgroundColor = HexColor("#D3E4FF")
expandBtn.setTitleColor(HexColor("#2076FF"), for: .normal)
expandBtn.setTitle("视频", for: .normal)
expandBtn.layer.cornerRadius = 16
expandBtn.clipsToBounds = true
expandBtn.titleLabel?.font = BoldFont_13
expandBtn.frame = CGRect(x: 0, y: 0, width: 60, height: 32)
expandBtn.addTarget(self, action: #selector(headerDuanJuBtnAction), for: .touchUpInside)
return expandBtn
}()
func addRefreshView() {
resultTableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {[weak self] in
guard let self = self else { return }
self.page = 1
self.getSearchResultWithSearchText(searchText: self.searchText, modelId: self.isJujiModel ? "album" : "1", isFirst: true, page: "1")
})
resultTableView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {[weak self] in
guard let self = self else { return }
//"album"剧集 1单集
self.page = self.page + 1
self.getSearchResultWithSearchText(searchText: searchText, modelId: self.isJujiModel ? "album" : "1", isFirst: false, page: "\(self.page)")
})
}
func setUpResultHeaderView(){
resultHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: KSreenWidth, height: 60))
resultHeaderView?.backgroundColor = .white
resultHeaderView!.addSubview(headerDuanJuBtn)
headerDuanJuBtn.snp.makeConstraints { make in
make.left.equalTo(15)
make.width.equalTo(60)
make.centerY.equalToSuperview()
make.height.equalTo(32)
}
resultHeaderView!.addSubview(headerJuJiBtn)
headerJuJiBtn.snp.makeConstraints { make in
make.left.equalTo(self.headerDuanJuBtn.snp.right)
make.width.equalTo(100)
make.centerY.equalToSuperview()
make.height.equalTo(32)
}
resultTableView.tableHeaderView = resultHeaderView
self.isJujiModel = false
}
func changeResult(){
self.page = 1
self.getSearchResultWithSearchText(searchText: self.searchText, modelId: self.isJujiModel ? "album" : "1", isFirst: true, page: "1")
}
// MARK: - BtnAction
@objc func headerDuanJuBtnAction(){
if self.searBarView.isFirstResponder{
self.view.endEditing(true)
}
isJujiModel = false
headerJuJiBtn.backgroundColor = .clear
headerJuJiBtn.setTitleColor(HexColor("#919191"), for: .normal)
headerDuanJuBtn.backgroundColor = HexColor("#D3E4FF")
headerDuanJuBtn.setTitleColor(HexColor("#2076FF"), for: .normal)
changeResult()
}
@objc func headerJuJiBtnAction(){
if self.searBarView.isFirstResponder{
self.view.endEditing(true)
}
isJujiModel = true
headerJuJiBtn.backgroundColor = HexColor("#D3E4FF")
headerJuJiBtn.setTitleColor(HexColor("#2076FF"), for: .normal)
headerDuanJuBtn.backgroundColor = .clear
headerDuanJuBtn.setTitleColor(HexColor("#919191"), for: .normal)
changeResult()
}
@objc func cancelBtnAction(){
self.isJujiModel = false
if self.resultTableView.isHidden{
self.navigationController?.popViewController(animated: false)
}else{
self.resultTableView.isHidden = true
}
}
// MARK: - UISearchBarDelegate
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let text = searchBar.text{
let trimStr = text.qmui_trim
if trimStr.qmui_trimAllWhiteSpace.count == 0{
QMUITips.showError("搜索内容不能为空", in: self.view, hideAfterDelay: 1.5)
return
}
self.tableView.tableFooterView = nil
self.tableView.mj_footer = nil
self.searchText = text
self.page = 1
self.view.endEditing(true)
showLoading(message: "")
self.getSearchResultWithSearchText(searchText: searchText, modelId: self.isJujiModel ? "album" : "1", isFirst: true, page: "1")
self.recordSearchHistory()
self.getSearchHotListApi()
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.count == 0{
self.tableView.tableFooterView = nil
// self.tableView.removeFromSuperview()
}
}
func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "" || text == "\n"{
//删
return true
}
if (text.count+(searchBar.text?.count ?? 0)) > 30{
//输入字符超过最大限制
QMUITips.showError("输入字符超过最大限制", in: self.view, hideAfterDelay: 1.5)
return false
}
return true
}
// MARK: - UITableViewDelegate
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.tableView{
return self.dataArray.count
}else{
//搜索结果
return self.resultListArray.count
}
}
// MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == self.tableView{
if let historyModel = self.dataArray[indexPath.row] as? CNLiveHomeSearchHistoryModel{
//搜索历史
guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveHomeSearchHistoryCellIdentifier) as? CNLiveHomeSearchHistoryCell
else {
let cell = CNLiveHomeSearchHistoryCell()
return cell
}
cell.historyModel = historyModel
cell.searchHistoryClickBlock = { searchText in
self.searchText = searchText
self.view.endEditing(true)
self.getSearchResultWithSearchText(searchText: searchText, modelId: self.isJujiModel ? "album" : "1", isFirst: true, page: "1")
}
cell.newHeight = { height in
self.historyHeight = height
}
return cell
}else if let hotModel = self.dataArray[indexPath.row] as? CNLiveHomeSearchHotModel{
//热词
guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveHomeSearchHotCellIdentifier) as? CNLiveHomeSearchHotCell
else {
let cell = CNLiveHomeSearchHotCell()
return cell
}
cell.hotModel = hotModel
return cell
}else{
}
}else{
//搜索结果
if let resModel = self.resultListArray[indexPath.row] as? CNLiveHomeSearchResultListModel{
guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveHomeSearchResultJuJiCellIdentifier) as? CNLiveHomeSearchResultJuJiCell
else {
let cell = CNLiveHomeSearchResultJuJiCell()
return cell
}
cell.resModel = resModel
return cell
// else if resModel.model == kCNDownSlideContentsModelModel1{//点播
// guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveHomeSearchResultDuanJuCellIdentifier) as? CNLiveHomeSearchResultDuanJuCell
// else {
// let cell = CNLiveHomeSearchResultDuanJuCell()
// return cell
// }
// cell.resModel = resModel
// return cell
// }
}
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView == self.tableView{
if let slideModel = self.dataArray[indexPath.row] as? CNLiveHomeSearchHotModel{
//热词列表
self.searchText = slideModel.value.count > 0 ? slideModel.value : ""
self.searBarView.text = self.searchText
self.recordSearchHistory()
self.getSearchHotListApi()
self.getSearchResultWithSearchText(searchText: self.searchText, modelId: self.isJujiModel ? "album" : "1", isFirst: true, page: "1")
}
}else{
//搜索结果
if let resModel = self.resultListArray[indexPath.row] as? CNLiveHomeSearchResultListModel{
var contentId = ""
var pid = ""
var title = ""
if resModel.model == kCNDownSlideContentsModelModelAlbum_video{//专辑
contentId = resModel.albumId
pid = resModel.pid
title = resModel.name
}else if resModel.model == kCNDownSlideContentsModelModel1{//点播
contentId = resModel.contentId
pid = resModel.pid
title = resModel.title
}
//跳转到点播详情页-专辑/点播
CNLiveDownSlideTool.pushWebAndVodVCWithContentsMdl(model: resModel.model, contentId: contentId, pid: pid, shareUrl: "", thirdUrl: "", title: title, s_to: "", trailerId: "", controller: navigationViewController().visibleViewController, slideModel: nil)
}else{
}
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if tableView == self.tableView{
if let slideModel = self.dataArray[indexPath.row] as? CNLiveHomeSearchHotModel{
//热词列表
// let slideModel = self.dataArray[indexPath.row] as! CNLiveHomeSearchHotModel
return 40
}else if let slideModel = self.dataArray[indexPath.row] as? CNLiveHomeSearchHistoryModel{
//搜索历史
let slideModel = self.dataArray[indexPath.row] as! CNLiveHomeSearchHistoryModel
return self.historyHeight//176//145
// return CGFloat(slideModel.cellH)
}else{
return 170
}
}else{
//搜索结果
//albnum剧集90
//1点播 imgH+55 (imgH+55)i -10
return 90
// if let resModel = self.resultListArray[indexPath.row] as? CNLiveHomeSearchResultListModel{
// return 90
// if resModel.model == kCNDownSlideContentsModelModelAlbum_video{//专辑
// return 90
// }else if resModel.model == kCNDownSlideContentsModelModel1{//点播
// let imgW = (KSreenWidth-4*15.0)/3.0
// let imgH = imgW*140.0/105.0
// return imgH+55
// }else{
//
// }
// }else{//专辑或单集
// return 60
// }
// return 60
}
}
func recordSearchHistory(){
if var hisArr = UserDefaults.standard.array(forKey: CNLiveHomeSearchHistoryKey) as? Array<String>{
var hisArrM = hisArr
if self.searchText.count > 0{
if hisArrM.contains(self.searchText){
hisArrM.remove(self.searchText)
hisArrM.append(self.searchText)
}else{
if hisArrM.count >= 10{
hisArrM.remove(at: 0)
}
hisArrM.append(self.searchText)
}
UserDefaults.standard.setValue(hisArrM, forKey: CNLiveHomeSearchHistoryKey)
UserDefaults.standard.synchronize()
}
self.hisArr.removeAll()
for his in hisArrM {
self.hisArr.append(his)
}
let hisModel = CNLiveHomeSearchHistoryModel()
hisModel.history = self.hisArr
hisModel.cellH = 140//计算高度
self.dataArray.removeAllObjects()
self.dataArray.add(hisModel)
}else{
let tempArr = [String]()
UserDefaults.standard.setValue(tempArr, forKey: CNLiveHomeSearchHistoryKey)
UserDefaults.standard.synchronize()
self.hisArr = [String]()
let hisModel = CNLiveHomeSearchHistoryModel()
hisModel.history = self.hisArr
hisModel.cellH = 140//计算高度
self.dataArray.removeAllObjects()
self.dataArray.add(hisModel)
}
self.tableView.reloadData()
}
func getSearchHotListApi(){
CNLiveHomeSearchViewModel.getSearchHotListApi {[weak self] result, status in
guard let self = self else { return }
if status == .success{
if let hotArr = result as? [CNLiveHomeSearchHotModel]{
//热词
let titleMdl = CNLiveHomeSearchHotModel()
titleMdl.sort = 0
titleMdl.value = "热点榜"
self.dataArray.add(titleMdl)
for (idx,mdl) in hotArr.enumerated() {
mdl.sort = idx+1
self.dataArray.add(mdl)
}
self.tableView.mj_header?.endRefreshing()
self.tableView.reloadData()
}
}else{
}
}
}
//获取搜索结果
func getSearchResultWithSearchText(searchText:String,modelId:String,isFirst:Bool,page:String){
self.searBarView.text = searchText
CNLiveHomeSearchViewModel.getSearchResultApi(modelId: modelId, keyword: searchText, page: page, pageSize: "10") { [weak self] result, status in
guard let self = self else { return }
self.resultTableView.isHidden = false
if status == .success{
if let resultBaseModel = result as? CNLiveHomeSearchResultListBaseModel{
if self.page > 1 {
for mdl in resultBaseModel.list{
self.resultListArray.add(mdl)
}
}
if self.page == 1 {
self.resultListArray.removeAllObjects()
for mdl in resultBaseModel.list{
self.resultListArray.add(mdl)
}
if self.resultListArray.count > 0 && self.resultTableView.mj_header == nil && self.resultTableView.mj_footer == nil {
self.addRefreshView()
self.page = self.page + 1
self.getSearchResultWithSearchText(searchText: searchText, modelId: self.isJujiModel ? "album" : "1", isFirst: true, page: "\(self.page)")
}
}
self.resultTableView.mj_header?.endRefreshing()
self.resultTableView.mj_footer?.endRefreshing()
self.resultTableView.reloadData()
if resultBaseModel.list.count == 0 {
self.resultTableView.mj_footer?.endRefreshingWithNoMoreData()
self.resultTableView.reloadData()
}
if resultBaseModel.hasNextPage == false{
self.resultTableView.mj_footer?.endRefreshingWithNoMoreData()
self.resultTableView.reloadData()
}
}
}else{
}
}
}
deinit {
}
}
//import UIKit
//import MJRefresh
//import QMUIKit
//import SnapKit // 假设你使用 SnapKit 进行布局
//
//class CNClassifySearchController: BaseViewController {
//
// // 声明属性
// var refreshView: MJRefreshAutoNormalFooter!
// var listArrM = :ml-search[Any]
// var hasNextPage: String?
// var page = 1
// var hideSearchKB = false
//
// // 搜索历史相关
// var titleLab: UILabel!
// var labelLineView: UIView!
// var scrollView: UIScrollView!
// var historyItemView: CNCategoryFloatLayoutView! // 假设这是一个自定义的 UIView 子类
// var clearButton: UIButton!
//
// // 其他必要的属性...
//
// override func viewDidLoad() {
// super.viewDidLoad()
//
// // 初始化列表和页面
// listArrM = []
// page = 1
//
// // 设置刷新控件
// refreshView = MJRefreshAutoNormalFooter { [weak self] in
// guard let self = self else { return }
// let pageStr = String(self.page)
// self.getClassifySearchResult(withSearchText: self.searchText, isFirst: false, page: pageStr)
// }
//
// // 设置头部刷新控件(假设这是自定义的)
// // tableView.mj_header = ...
//
// // 如果不是从全搜索来的,则设置 UI
// if !fromAllSearch {
// setupUI()
// }
//
// // 监听键盘隐藏通知
// NotificationCenter.default.addObserver(self, selector: #selector(keyBoardHidden), name: UIKeyboardDidHideNotification, object: nil)
// }
//
// // MARK: - 视图生命周期方法
//
// override func viewWillAppear(_ animated: Bool) {
// super.viewWillAppear(animated)
// navigationController?.navigationBar.isHidden = true
// }
//
// override func viewDidAppear(_ animated: Bool) {
// super.viewDidAppear(animated)
// navigationController?.navigationBar.isHidden = true
// if hideSearchKB {
// hideKB()
// }
// hideSearchKB = true
// searchBar.becomeFirstResponder()
// }
//
// override func viewWillDisappear(_ animated: Bool) {
// super.viewWillDisappear(animated)
// if !fromAllSearch {
// navigationController?.navigationBar.isHidden = false
// }
// view.endEditing(true)
// }
//
// override func viewDidDisappear(_ animated: Bool) {
// super.viewDidDisappear(animated)
// if !fromAllSearch {
// navigationController?.navigationBar.isHidden = false
// }
// }
//
// // MARK: - 搜索功能
//
// @objc func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
// let searchText = searchBar.text?.trimmingCharacters(in: .whitespacesAndNewlines)
// if searchText?.isEmpty ?? true {
// QMUITips.showError(in: view, message: "搜索内容不能为空", hideAfterDelay: 1.5)
// return
// }
//
// tableView.mj_footer = nil
// tableView.tableFooterView = nil
// self.searchText = searchText
// page = 1
// QMUITips.showLoading(in: view)
// getClassifySearchResult(withSearchText: searchText!, isFirst: true, page: "1")
//
// recordHistory()
// }
//
// // 其他搜索相关的方法...
//
// // MARK: - 清除搜索历史
//
// @objc func clearBtnClick(_ sender: UIButton) {
// tableView.mj_footer = nil
// tableView.tableFooterView = nil
// listArrM.removeAll()
// tableView.reloadData()
// // 可能需要移除 tableView
// }
//
// // MARK: - 获取搜索结果
//
// func getClassifySearchResult(withSearchText searchText: String, isFirst: Bool, page: String) {
// if isFirst && tableView.superview == nil {
// view.addSubview(tableView)
// }
//
// let param: [String: Any] = [
// "page": page.isEmpty ? "1" : page,
// "pageSize": "10",
// "keyword": searchText.urlEncoded // 假设这是一个自定义的扩展方法
// ]
//
// // 发起网络请求(这里使用假设的网络请求方法)
// // networkRequest(param) { response in
// // // 处理响应
// // }
//
// // 假设网络请求已经完成,并返回了结果
// // let responseObject = ...
//
// // 更新 UI(这里仅展示逻辑,实际代码需根据响应对象调整)
// tableView.mj_footer?.endRefreshing()
// tableView.mj_header?.endRefreshing()
// QMUITips.hideAllTips(in: view)
//
// if let dataDic = responseObject?["data"] as? [String: Any],
// let hasNextPageStr = dataDic["hasNextPage"] as? String,
// hasNextPageStr == "1" {
// page += 1
// } else {
// tableView.mj_footer?.endRefreshingWithNoMoreData()
// }
//
// // 根据不同类型更新 listArrM 和 tableView
// // ...
//
// if isFirst {
// tableView.reloadData()
// if listArrM.count > 0, hasNextPageStr == "1" {
// tableView.mj_footer = refreshView
// }
// tableView.scrollToTop(animated: true)
// }
// }
//
// // MARK: - 记录搜索历史
//
// func recordHistory() {
// // 假设 CNUserShareModel 是一个单例或全局对象
// let historyKey = "CNWjjCategorySearchHistoryKey_\(CNUserShareModel.shared.uid)"
// if let userDefaults = UserDefaults.standard,
// let historyArray = userDefaults.array(forKey: historyKey) as? [String] {
// var historyArrayM = historyArray.mutableCopy() as! [String]
// if let searchText = searchText, !historyArrayM.contains(searchText) {
// if historyArrayM.count >= 10 {
// historyArrayM.removeFirst()
// }
// historyArrayM.append(searchText)
// }
// userDefaults.set(historyArrayM, forKey: historyKey)
// userDefaults.synchronize()
//
// historyItemView.subviews.forEach { $0.removeFromSuperview() }
// historyArrayM.reversed().forEach { str in
// let button = QMUIGhostButton()
// button.ghostColor = .black
// button.layer.borderColor = UIColor(rgb: 0xD4D5DA).cgColor
// button.setTitle(str, for: .normal)
// button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
// button.contentEdgeInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
// button.addTarget(self, action: #selector(historyButtonAction(_:)), for: .touchUpInside)
// historyItemView.addSubview(button)
// }
//
// historyItemView.snp.remakeConstraints { make in
// make.top.equalTo(scrollView)
// make.left.equalTo(view).offset(10)
// make.right.equalTo(view).offset(-10)
// make.height.equalTo(historyItemView.sizeThatFits(CGSize(width: view.bounds.width - 20, height: CGFloat.greatestFiniteMagnitude)).height)
// }
//
// let offHeight = historyItemView.snp.frame.height + 10 + 45 + 10 + UIApplication.shared.statusBarFrame.height + 109 - view.bounds.height
// if offHeight > 0 {
// scrollView.contentSize = CGSize(width: view.bounds.width, height: view.bounds.height - UIApplication.shared.statusBarFrame.height - 108 + offHeight)
// }
//
// clearButton.isHidden = historyArrayM.isEmpty
// }
// }
//
// // MARK: - 显示无内容页
//
// func showEmptyView() {
// tableView.mj_footer = nil