// // SearchViewController.swift // MTP2_iOS // // Created by Handy_Cao on 2020/11/12. // Copyright © 2020 Muchinfo. All rights reserved. // import UIKit /// 商品检索视图容器控制类 class SearchViewController: BaseViewController { // MARK: - 属性列表 /// 数据展示列表 @IBOutlet weak var tableView: UITableView! /// 历史检索视图 @IBOutlet weak var collectionView: UICollectionView! { didSet { /// 设置约束 collectionView.setCollectionViewLayout(flowLayout, animated: true) } } /// 高度约束 @IBOutlet weak var heightLayoutConstraint: NSLayoutConstraint! /// 删除按钮 @IBOutlet weak var delete: UIButton! /// 检索视图 lazy var searchBar: UISearchBar = { $0.layer.cornerRadius = 10.0 if #available(iOS 13.0, *) { $0.searchTextField.backgroundColor = UIColorFromHex(rgbValue: 0xf0f0f0) } else { // Fallback on earlier versions $0.barTintColor = UIColorFromHex(rgbValue: 0xf0f0f0) } $0.delegate = self $0.showsCancelButton = false $0.tintColor = .white $0.placeholder = "请输入商品名称或者编号" return $0 }(UISearchBar()) /// 数据显示集合视图约束 lazy var flowLayout: UICollectionViewFlowLayout = { /// 最小行间距,默认是0 $0.minimumLineSpacing = 10 /// 最小左右间距,默认是10 $0.minimumInteritemSpacing = 10 /// 区域内间距,默认是 UIEdgeInsetsMake(0, 0, 0, 0) $0.sectionInset = UIEdgeInsets(top: 5.0, left: 5, bottom: 5, right: 5) /// 水平滚动 $0.scrollDirection = .vertical return $0 } (UICollectionViewFlowLayout()) /// CellIdentifier let CellIdentifier: String = "SearchGoods_Cell" /// HisCellIdentifier let HisCellIdentifier: String = "HisSearchGoods_Cell" /// 检索商品信息 var searchGoods: [MoSearchGoods] = [] { didSet { /// 刷新列表数据 tableView.reloadData() } } /// 历史记录 var hisResults: [String] { get { UserDefaultsUtils.getResearchGoodsInfo() } } /// 交易模式 var tradeMode: ETradeMode? // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. /// 检索视图 self.navigationItem.titleView = self.searchBar /// 数据初始化 self.initData() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) /// 显示导航栏 self.navigationController?.setNavigationBarHidden(false, animated: true) } // MARK: - 数据初始化 /// 数据初始化 fileprivate func initData() { /// 刷新数据 self.collectionView.reloadData() /// 设置动态高度约束 self.heightLayoutConstraint.constant = hisResults.count > 0 ? 90.0 : 0.0 } // MARK: - 交互相关 @IBAction fileprivate func onBarButtonItemPressed(_ sender: UIBarButtonItem) { /// 失去第一次响应者 self.searchBar.resignFirstResponder() self.searchBar.text = "" /// 赋值历史 self.collectionView.reloadData() /// 设置动态高度约束 self.heightLayoutConstraint.constant = hisResults.count > 0 ? 90.0 : 0.0 } /// onButtonPressed /// - Parameter sender: sender @IBAction fileprivate func onButtonPressed(_ sender: UIButton) { switch sender { case delete: /// 删除 DispatchQueue.main.async { UserDefaultsUtils.deleteAllResearchGoodsInfo() /// 数据初始化 self.initData() } default: break } } // MARK: - 接口请求 /// 商品检索数据信息 /// - Parameter string: 检索内容 func requestSearchGoods(_ string: String) { /// 异常 guard let goodsManager = MTP2BusinessCore.shared.goodsManager else { return } /// 商品检索 goodsManager.requestSearchGoodses(string, "16,70,71") { (isComplete, error, goods) in DispatchQueue.main.async { self.searchGoods = goods ?? [] } } } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */ } // MARK: - UITableViewDelegate, UITableViewDataSource extension SearchViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.searchGoods.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) cell.textLabel?.text = self.searchGoods[indexPath.row].goodsname cell.detailTextLabel?.text = self.searchGoods[indexPath.row].goodscode return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { /// 添加检索商品信息 UserDefaultsUtils.setResearchGoodsInfo(searchBar.text ?? "") searchBar.resignFirstResponder() /// 商品详情 guard let goods = MTP2BusinessCore.shared.goodsManager?.goodsInfos.first(where: {$0.goodsid == self.searchGoods[indexPath.row].goodsid}) else { return } /// 商城商品 if goods.trademode == .TRADEMODE_TRADEMODE_HSBY_SHOP { let specialSaleController = viewController(name: "Quote", identifier: "SpecialSale") as! SpecialSaleViewController specialSaleController.goodsid = goods.goodsid specialSaleController.marketid = "\(goods.marketid)" self.navigationController?.pushViewController(specialSaleController, animated: true) specialSaleController.initData() } else { let goodsDetailController = UIStoryboard(name: "Quote", bundle: nil).instantiateViewController(withIdentifier: "GoodsDetail") as! GoodsDetailViewController goodsDetailController.model = goods self.navigationController?.pushViewController(goodsDetailController, animated: true) } } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 44.0 } } // MARK: - UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout extension SearchViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return hisResults.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HisCellIdentifier, for: indexPath) as! SearchCell cell.textLabel.text = hisResults[indexPath.row] return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { self.searchBar.text = hisResults[indexPath.row] UIView.animate(withDuration: 0.5) { self.heightLayoutConstraint.constant = 0.0 } completion: { (finished) in if finished { /// 查询数据 self.requestSearchGoods(self.hisResults[indexPath.row]) } } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let width = hisResults[indexPath.row].getTextSize(font: .font_12, size: .zero).width+10.0 return CGSize(width: width > 60.0 ? width : 60.0, height: 30.0) } } // MARK: - UITableViewDelegate, UISearchBarDelegate extension SearchViewController: UISearchBarDelegate { func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { /// 根据结果检索信息 self.requestSearchGoods(searchBar.text ?? "") } } // MARK: - SearchCell class SearchCell: UICollectionViewCell { /// 标题 @IBOutlet weak var textLabel: UILabel! { didSet { textLabel.layer.cornerRadius = 10.0 textLabel.layer.masksToBounds = true } } }