Commit 4115c1ea authored by David黄金龙's avatar David黄金龙
parents 659cf11f c9c1ee53
......@@ -22,31 +22,20 @@ class YHSearchInfomationVC: YHBaseViewController {
bar.searchBlock = {
[weak self] text in
guard let self = self else { return }
self.viewModel.searchArticleList(text ?? "") {
[weak self] success, error in
guard let self = self else { return }
searchText(self.searchBar.textField.text ?? "")
}
bar.textChange = {
[weak self] text in
guard let self = self else { return }
if isEmptyString(text) {
searchHistoryView.isHidden = false
items.removeAll()
if let arr = self.viewModel.arrHomeNewsData {
items.append(contentsOf: arr)
}
self.tableView.reloadData()
}
}
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 = {
let tableView = UITableView(frame:.zero, style:.grouped)
......@@ -61,16 +50,57 @@ class YHSearchInfomationVC: YHBaseViewController {
tableView.delegate = self
tableView.dataSource = self
tableView.backgroundColor = .white
tableView.backgroundView = emptyDataTipsView
tableView.register(YHSearchInfoCell.self, forCellReuseIdentifier: YHSearchInfoCell.cellReuseIdentifier)
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() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(searchBar)
view.addSubview(searchHistoryView)
view.addSubview(tableView)
view.addSubview(searchHistoryView)
searchBar.textField.becomeFirstResponder()
searchBar.snp.makeConstraints { make in
......@@ -92,6 +122,22 @@ class YHSearchInfomationVC: YHBaseViewController {
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 {
......@@ -122,9 +168,11 @@ extension YHSearchInfomationVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if 0 <= indexPath.section && indexPath.section < items.count {
let articleInfo = items[indexPath.section]
self.tableView.isHidden = true
self.searchHistoryView.isHidden = false
let model = items[indexPath.section]
let vc = YHHomeWebViewController()
vc.url = YHConfigManager.shared.h5Url + "?id=\(model.id)"
vc.id = model.id
self.navigationController?.pushViewController(vc)
}
}
......
......@@ -67,6 +67,7 @@ class YHSearchInfoBar: UIView {
}
@objc func searchBtnClicked() {
textField.resignFirstResponder()
if let searchBlock = searchBlock {
searchBlock(textField.text)
}
......@@ -80,7 +81,7 @@ class YHSearchInfoBar: UIView {
func createUI() {
self.backgroundColor = .white
setSearchButtonEnable(false)
setSearchButtonEnable(true)
contentView.addSubview(searchImgView)
contentView.addSubview(textField)
......@@ -111,11 +112,6 @@ class YHSearchInfoBar: UIView {
}
@objc func textFieldChanged(textField:UITextField) {
if isEmptyString(textField.text) {
setSearchButtonEnable(false)
} else {
setSearchButtonEnable(true)
}
if let textChange = textChange {
textChange(textField.text)
}
......@@ -130,7 +126,7 @@ extension YHSearchInfoBar: UITextFieldDelegate {
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 {
YHHUD.flash(message: "搜索限制最多\(Self.maxWordsCount)个字符")
return false
......
......@@ -31,6 +31,13 @@ class YHSearchInfoCell: UITableViewCell {
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 = {
var label = UILabel()
label.font = .PFSC_R(ofSize: 12)
......@@ -42,6 +49,7 @@ class YHSearchInfoCell: UITableViewCell {
func updateModel(_ model: YHHomeListModel) {
titleLabel.text = model.title
videoPlayImgView.isHidden = (model.type != 1)
if let url = URL(string: model.img_url) {
headImgView.kf.setImage(with: url)
}
......@@ -51,6 +59,7 @@ class YHSearchInfoCell: UITableViewCell {
contentView.backgroundColor = .white
contentView.addSubview(headImgView)
headImgView.addSubview(videoPlayImgView)
contentView.addSubview(titleLabel)
headImgView.snp.makeConstraints { make in
......@@ -59,6 +68,11 @@ class YHSearchInfoCell: UITableViewCell {
make.left.equalToSuperview().offset(20)
}
videoPlayImgView.snp.makeConstraints { make in
make.width.height.equalTo(20.0)
make.center.equalToSuperview()
}
titleLabel.snp.makeConstraints { make in
make.centerY.equalToSuperview()
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