// // IHAreaSeachView.swift // Inhealth // // Created by weclouds on 2019/12/27. // Copyright © 2019 weclouds. All rights reserved. // import UIKit protocol IHAreaSearchViewDelegate : NSObjectProtocol{ func searchViewDidSelectRowAt(indexPath: IndexPath) } class IHAreaSeachView: UIView { weak var viewDelegate : IHAreaSearchViewDelegate? var target : UIViewController? var roomList : [RoomData]? { didSet{ self.tableView.reloadData() } } var fillterResult :[RoomData]?{ didSet{ self.tableView.reloadData() } } // MARK: - Cancel Button 取消按钮 public var showCancelButtonWhileEditing: Bool = false var searchController : UISearchController? lazy var tableView: UITableView = { let tableView = UITableView(frame: CGRect.zero, style: .plain) tableView.delegate = self tableView.dataSource = self tableView.backgroundColor = UIColor(hexString: "#F6F8F7") tableView.separatorStyle = .none // tableView.register(UINib(nibName: "IHAreaListCell", bundle: nil), forCellReuseIdentifier: "cell") return tableView }() override init(frame: CGRect) { super.init(frame: frame) addSubview(tableView) settingSerachController() } override func layoutSubviews() { super.layoutSubviews() self.tableView.frame = self.bounds } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func settingSerachController() { //传nil 搜索代码在当前界面 searchController = UISearchController(searchResultsController: nil) searchController?.searchResultsUpdater = self searchController?.searchBar.frame = CGRect(x: 0, y: 0, width: 0, height: 44) searchController?.dimsBackgroundDuringPresentation = false searchController?.hidesNavigationBarDuringPresentation = true searchController?.obscuresBackgroundDuringPresentation = false searchController?.searchBar.sizeToFit() searchController?.searchBar.delegate = self searchController?.delegate = self //target?.navigationItem.searchController = searchController target?.definesPresentationContext = true tableView.tableHeaderView = searchController?.searchBar setSearchBar() } ///设置searchbar func setSearchBar() { //获取searchbar let searchBar = searchController?.searchBar searchBar?.barStyle = .default //获取searchBar 的search let searchField = searchBar?.value(forKey: "searchField") as? UITextField if searchField == nil { return } //文本框字体颜色 searchField?.font = UIFont(name: PingFangSC_Regular, size: 14) searchField?.textColor = UIColor(hexString: "#333333") ///文本框文字颜色 let holderText = "请输入房间号" let placeholder = NSMutableAttributedString(string: holderText) placeholder.addAttributes([NSAttributedString.Key.foregroundColor : UIColor(hexString: "#C6CDD5") as Any], range: NSRange(location: 0, length: holderText.count)) placeholder.addAttributes([NSAttributedString.Key.font : UIFont(name: Alibaba_PuHuiTi_Regular, size: 14) as Any], range: NSRange(location: 0, length: holderText.count)) searchField?.attributedPlaceholder = placeholder //设置文本框背景 searchField?.backgroundColor = UIColor(hexString: "#F6F8F7") //隐藏搜索框的上下两条黑线 searchBar?.backgroundImage = UIImage() //搜索框背景颜色 searchBar?.barTintColor = UIColor(hexString: "#F6F8F7") searchBar?.tintColor = UIColor(hexString: "#657085") } ///设置取消按钮 ///过滤 func filterContent(for searchText:String) { let _roomlist = self.roomList let room_number = searchText var fillterRoomNS : [RoomData] if room_number == "" || _roomlist == nil || _roomlist?.count == 0 { //没有输入则为传入的内容 fillterRoomNS = _roomlist! }else{ fillterRoomNS = (_roomlist?.filter({ (newRoom) -> Bool in let number = newRoom.number! let search = searchText.lowercased() return number.contains(search) == true }))! } fillterResult = fillterRoomNS } } extension IHAreaSeachView :UITableViewDataSource,UITableViewDelegate{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if searchController?.isActive == true { return self.fillterResult?.count ?? 0 }else{ return self.roomList?.count ?? 0 } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { //这里要判断是学校还是酒店 let room = searchController?.isActive == true ? self.fillterResult![indexPath.row] : self.roomList![indexPath.row] var cell : IHAreaListCell? if room.type == "1"{ ////学校 ClassRoom cell = tableView.dequeueReusableCell(withIdentifier: "ClassRoom") as? IHAreaListCell if cell == nil{ cell = Bundle.main.loadNibNamed("IHAreaListCell", owner: nil, options: nil)?.last as? IHAreaListCell } }else{ //酒店 cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? IHAreaListCell if cell == nil{ cell = Bundle.main.loadNibNamed("IHAreaListCell", owner: nil, options: nil)?.first as? IHAreaListCell } } cell?.selectionStyle = .none cell?.room = room return cell! } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 150 } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // self.endEditing(true) searchController?.isActive = false if let delegate = viewDelegate { delegate.searchViewDidSelectRowAt(indexPath: indexPath) } } } extension IHAreaSeachView : UISearchResultsUpdating,UISearchBarDelegate,UISearchControllerDelegate{ func updateSearchResults(for searchController: UISearchController) { for view in searchController.searchBar.subviews{ log.debug("view = \(view)") for sub in view.subviews[0].subviews { log.debug("sub \(sub)") } } if let searchText = searchController.searchBar.text { filterContent(for: searchText) } } func didPresentSearchController(_ searchController: UISearchController) { log.debug("self.tableView.frame - \(self.tableView.frame)") showCancelButtonWhileEditing = true } func willDismissSearchController(_ searchController: UISearchController) { log.debug("self.tableView.frame - \(self.tableView.frame)") showCancelButtonWhileEditing = false } }