Commit c9c1ee53 authored by Steven杜宇's avatar Steven杜宇

// 搜索

parent 2f3c1e45
...@@ -22,31 +22,20 @@ class YHSearchInfomationVC: YHBaseViewController { ...@@ -22,31 +22,20 @@ class YHSearchInfomationVC: YHBaseViewController {
bar.searchBlock = { bar.searchBlock = {
[weak self] text in [weak self] text in
guard let self = self else { return } guard let self = self else { return }
self.viewModel.searchArticleList(text ?? "") { searchText(self.searchBar.textField.text ?? "")
[weak self] success, error in }
guard let self = self else { return } bar.textChange = {
[weak self] text in
guard let self = self else { return }
if isEmptyString(text) {
searchHistoryView.isHidden = false
items.removeAll() items.removeAll()
if let arr = self.viewModel.arrHomeNewsData {
items.append(contentsOf: arr)
}
self.tableView.reloadData() self.tableView.reloadData()
} }
} }
return bar return bar
}() }()
lazy var searchHistoryView: YHSearchInfoHistoryView = {
let view = YHSearchInfoHistoryView(frame: CGRect(x: 0, y: searchBar.frame.maxY+8, width: KScreenWidth, height: KScreenHeight-searchBar.frame.maxY-8))
view.isHidden = true
view.selectBlock = {
[weak self] text in
guard let self = self else { return }
self.searchHistoryView.isHidden = true
self.tableView.isHidden = false
}
return view
}()
lazy var tableView: UITableView = { lazy var tableView: UITableView = {
let tableView = UITableView(frame:.zero, style:.grouped) let tableView = UITableView(frame:.zero, style:.grouped)
...@@ -61,16 +50,57 @@ class YHSearchInfomationVC: YHBaseViewController { ...@@ -61,16 +50,57 @@ class YHSearchInfomationVC: YHBaseViewController {
tableView.delegate = self tableView.delegate = self
tableView.dataSource = self tableView.dataSource = self
tableView.backgroundColor = .white tableView.backgroundColor = .white
tableView.backgroundView = emptyDataTipsView
tableView.register(YHSearchInfoCell.self, forCellReuseIdentifier: YHSearchInfoCell.cellReuseIdentifier) tableView.register(YHSearchInfoCell.self, forCellReuseIdentifier: YHSearchInfoCell.cellReuseIdentifier)
return tableView return tableView
}() }()
lazy var searchHistoryView: YHSearchInfoHistoryView = {
let view = YHSearchInfoHistoryView(frame: CGRect(x: 0, y: searchBar.frame.maxY+8, width: KScreenWidth, height: KScreenHeight-searchBar.frame.maxY-8))
view.selectBlock = {
[weak self] text in
guard let self = self else { return }
self.searchHistoryView.isHidden = true
self.searchBar.textField.text = text
searchText(text)
}
return view
}()
lazy var emptyDataTipsView:UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: 124))
view.isHidden = true
let imgView = UIImageView(image: UIImage(named: "service_center_no_data"))
imgView.contentMode = .scaleAspectFill
view.addSubview(imgView)
let label = UILabel()
label.textColor = UIColor.labelTextColor2
label.font = UIFont.PFSC_R(ofSize: 14)
label.textAlignment = .center
label.text = "无搜索结果".local
view.addSubview(label)
imgView.snp.makeConstraints { make in
make.top.equalToSuperview().offset(204)
make.width.height.equalTo(92)
make.centerX.equalTo(view)
}
label.snp.makeConstraints { make in
make.top.equalTo(imgView.snp.bottom).offset(12)
make.centerX.equalTo(view)
}
return view
}()
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
view.backgroundColor = .white view.backgroundColor = .white
view.addSubview(searchBar) view.addSubview(searchBar)
view.addSubview(searchHistoryView)
view.addSubview(tableView) view.addSubview(tableView)
view.addSubview(searchHistoryView)
searchBar.textField.becomeFirstResponder() searchBar.textField.becomeFirstResponder()
searchBar.snp.makeConstraints { make in searchBar.snp.makeConstraints { make in
...@@ -92,6 +122,22 @@ class YHSearchInfomationVC: YHBaseViewController { ...@@ -92,6 +122,22 @@ class YHSearchInfomationVC: YHBaseViewController {
make.bottom.equalToSuperview() make.bottom.equalToSuperview()
} }
} }
func searchText(_ text:String) {
YHHUD.show(.progress(message: "搜索中..."))
self.viewModel.searchArticleList(text) {
[weak self] success, error in
YHHUD.hide()
guard let self = self else { return }
items.removeAll()
if let arr = self.viewModel.arrHomeNewsData {
items.append(contentsOf: arr)
}
emptyDataTipsView.isHidden = (items.count > 0)
searchHistoryView.isHidden = true
self.tableView.reloadData()
}
}
} }
extension YHSearchInfomationVC: UITableViewDelegate, UITableViewDataSource { extension YHSearchInfomationVC: UITableViewDelegate, UITableViewDataSource {
...@@ -122,9 +168,11 @@ extension YHSearchInfomationVC: UITableViewDelegate, UITableViewDataSource { ...@@ -122,9 +168,11 @@ extension YHSearchInfomationVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if 0 <= indexPath.section && indexPath.section < items.count { if 0 <= indexPath.section && indexPath.section < items.count {
let articleInfo = items[indexPath.section] let model = items[indexPath.section]
self.tableView.isHidden = true let vc = YHHomeWebViewController()
self.searchHistoryView.isHidden = false vc.url = YHConfigManager.shared.h5Url + "?id=\(model.id)"
vc.id = model.id
self.navigationController?.pushViewController(vc)
} }
} }
......
...@@ -67,6 +67,7 @@ class YHSearchInfoBar: UIView { ...@@ -67,6 +67,7 @@ class YHSearchInfoBar: UIView {
} }
@objc func searchBtnClicked() { @objc func searchBtnClicked() {
textField.resignFirstResponder()
if let searchBlock = searchBlock { if let searchBlock = searchBlock {
searchBlock(textField.text) searchBlock(textField.text)
} }
...@@ -80,7 +81,7 @@ class YHSearchInfoBar: UIView { ...@@ -80,7 +81,7 @@ class YHSearchInfoBar: UIView {
func createUI() { func createUI() {
self.backgroundColor = .white self.backgroundColor = .white
setSearchButtonEnable(false) setSearchButtonEnable(true)
contentView.addSubview(searchImgView) contentView.addSubview(searchImgView)
contentView.addSubview(textField) contentView.addSubview(textField)
...@@ -111,11 +112,6 @@ class YHSearchInfoBar: UIView { ...@@ -111,11 +112,6 @@ class YHSearchInfoBar: UIView {
} }
@objc func textFieldChanged(textField:UITextField) { @objc func textFieldChanged(textField:UITextField) {
if isEmptyString(textField.text) {
setSearchButtonEnable(false)
} else {
setSearchButtonEnable(true)
}
if let textChange = textChange { if let textChange = textChange {
textChange(textField.text) textChange(textField.text)
} }
...@@ -130,7 +126,7 @@ extension YHSearchInfoBar: UITextFieldDelegate { ...@@ -130,7 +126,7 @@ extension YHSearchInfoBar: UITextFieldDelegate {
return true return true
} }
var newText = (textField.text! as NSString).replacingCharacters(in: range, with: string) let newText = (textField.text! as NSString).replacingCharacters(in: range, with: string)
if newText.count > Self.maxWordsCount { if newText.count > Self.maxWordsCount {
YHHUD.flash(message: "搜索限制最多\(Self.maxWordsCount)个字符") YHHUD.flash(message: "搜索限制最多\(Self.maxWordsCount)个字符")
return false return false
......
...@@ -31,6 +31,13 @@ class YHSearchInfoCell: UITableViewCell { ...@@ -31,6 +31,13 @@ class YHSearchInfoCell: UITableViewCell {
return imagV return imagV
}() }()
lazy var videoPlayImgView: UIImageView = {
let imagV = UIImageView()
imagV.image = UIImage(named: "home_play_btn")
imagV.isHidden = true
return imagV
}()
lazy var titleLabel: UILabel = { lazy var titleLabel: UILabel = {
var label = UILabel() var label = UILabel()
label.font = .PFSC_R(ofSize: 12) label.font = .PFSC_R(ofSize: 12)
...@@ -42,6 +49,7 @@ class YHSearchInfoCell: UITableViewCell { ...@@ -42,6 +49,7 @@ class YHSearchInfoCell: UITableViewCell {
func updateModel(_ model: YHHomeListModel) { func updateModel(_ model: YHHomeListModel) {
titleLabel.text = model.title titleLabel.text = model.title
videoPlayImgView.isHidden = (model.type != 1)
if let url = URL(string: model.img_url) { if let url = URL(string: model.img_url) {
headImgView.kf.setImage(with: url) headImgView.kf.setImage(with: url)
} }
...@@ -51,6 +59,7 @@ class YHSearchInfoCell: UITableViewCell { ...@@ -51,6 +59,7 @@ class YHSearchInfoCell: UITableViewCell {
contentView.backgroundColor = .white contentView.backgroundColor = .white
contentView.addSubview(headImgView) contentView.addSubview(headImgView)
headImgView.addSubview(videoPlayImgView)
contentView.addSubview(titleLabel) contentView.addSubview(titleLabel)
headImgView.snp.makeConstraints { make in headImgView.snp.makeConstraints { make in
...@@ -59,6 +68,11 @@ class YHSearchInfoCell: UITableViewCell { ...@@ -59,6 +68,11 @@ class YHSearchInfoCell: UITableViewCell {
make.left.equalToSuperview().offset(20) make.left.equalToSuperview().offset(20)
} }
videoPlayImgView.snp.makeConstraints { make in
make.width.height.equalTo(20.0)
make.center.equalToSuperview()
}
titleLabel.snp.makeConstraints { make in titleLabel.snp.makeConstraints { make in
make.centerY.equalToSuperview() make.centerY.equalToSuperview()
make.left.equalTo(headImgView.snp.right).offset(20) make.left.equalTo(headImgView.snp.right).offset(20)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment