// // IHEquipmentSearchView.swift // Inhealth // // Created by weclouds on 2020/1/13. // Copyright © 2020 weclouds. All rights reserved. // import UIKit import ESPullToRefresh protocol IHEquipmentSearchViewDelegate : NSObjectProtocol{ //查找设备 func searchDevice(_ keyWords : String?) //选择网关 func equipmentListSelected(_ gateway: GatewayData?) //选择设备(学校/酒店) func equipmentListSelected(_ devData: DevData?) //左滑 - 设备(设置) func equipmentSetting(_ devData: DevData, segmentIndex: Int) //左滑 - 网关(设置) func gatewaySetting(_ gateway: GatewayData, segmentIndex setmentIndex: Int) //左滑 - 网关(升级) func gatewayUpdate(_ gateway : GatewayData,segmentIndex: Int) //左滑 - 设备(历史) func equipmentHistory(_ devData: DevData, segmentIndex: Int) //左滑 - 设备(删除) func equipmentListDeleteDevice(_ devType: String, devId: String,segmentIndex: Int) //左滑 - 网关(删除) func gatewayDelegate(_ gateway: GatewayData, segmentIndex: Int) func tableviewHeaderEsReloadData(keywords : String?)//上拉刷新 func tableviewBottonEsgetMoreData(keywords : String?)//下拉刷新 } class IHEquipmentSearchView: UIView { //weak var delegate : IHEquipmentlistViewDelegate? weak var delegate : IHEquipmentSearchViewDelegate? weak var target : IHEquipmentSeachVCtr?{ didSet{ //点击cell时,可以隐藏searchbar target?.definesPresentationContext = true } } private var searchText : String? //搜索输入的内容 private var totalDeviceList : [AnyObject]? //所有的设备 //查找返回的设备 var searchSchoolAndHotelList : [DevData]? //查找返回的网关 var searchGatewayList : [GatewayData]? //刷新列表 var reloadAll : Bool?{ didSet{ var list = [AnyObject]() if searchSchoolAndHotelList?.count ?? 0 > 0{ for item in searchSchoolAndHotelList! { list.append(item as AnyObject) } } if searchGatewayList?.count ?? 0 > 0{ for item in searchGatewayList! { list.append(item as AnyObject) } } totalDeviceList = list self.tableView.reloadData() } } var devDataList:[DevData]?{ didSet{ self.tableView.reloadData() } } var gatewayList:[GatewayData]?{ didSet{ self.tableView.reloadData() } } var currentIndex :Int? = 0 var fillterResult :[DevData]?{ didSet{ self.tableView.reloadData() } } var fillterGatewayResult:[GatewayData]?{ didSet{ self.tableView.reloadData() } } // MARK: - Cancel Button 取消按钮 public var showCancelButtonWhileEditing: Bool = false private var isRemove = false //是否移除了上下拉刷新 var searchController : UISearchController? //编辑的cell var editingIndexPath : IndexPath? lazy var tableView: UITableView = { let tableView = UITableView(frame: .zero, style: .plain) tableView.delegate = self tableView.dataSource = self tableView.allowsSelection = true tableView.separatorStyle = .none tableView.register(UINib(nibName: "IHEquipmentListCell", bundle: nil), forCellReuseIdentifier: "cell") return tableView }() override init(frame: CGRect) { super.init(frame: frame) // self.backgroundColor = .red addSubview(tableView) settingSerachController() //加载上下拉刷新 tableViewRefresh() } private func tableViewRefresh(){ let header = KKWRefreshHeaderAnimator(frame: CGRect.zero) let footer = ESRefreshFooterAnimator(frame: CGRect.zero) //下拉刷新 tableView.es.addPullToRefresh(animator: header) {[unowned self] in log.debug("下ll拉加载更多") self.delegate?.tableviewHeaderEsReloadData(keywords :self.searchText) } //上拉加载更多 tableView.es.addInfiniteScrolling(animator: footer) {[unowned self] in log.debug("上拉加载更多") //上拉刷新 self.delegate?.tableviewBottonEsgetMoreData(keywords : self.searchText) } } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func endReflesh(){ tableView.es.stopPullToRefresh() tableView.es.stopLoadingMore() } override func layoutSubviews() { super.layoutSubviews() searchController?.view.frame = self.bounds tableView.frame = self.bounds // tableView.backgroundColor = .blue //加了上下拉刷新,就要加这个 tableView.y = 44 tableView.height = tableView.height - 44 if self.editingIndexPath != nil { configSwipeButtons() } } 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 //点击cell时,可以隐藏searchbar(这里此时的target是没有值的) // 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 = "请输入设备ID或设备名称" 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) { if currentIndex == 0 { if self.devDataList != nil && self.devDataList?.count != 0{ filterDevice(searchText) } if self.gatewayList != nil && self.gatewayList?.count != 0{ filterGateway(searchText) } }else if currentIndex == 5{ filterGateway(searchText) }else{ filterDevice(searchText) } } func filterDevice(_ searchText:String) { let _devicelist = self.devDataList let devId = searchText var fillterDevicesNS : [DevData] if devId == "" || _devicelist == nil || _devicelist?.count == 0 { //没有输入则为传入的内容 fillterDevicesNS = _devicelist! }else{ fillterDevicesNS = (_devicelist?.filter({ (newDevice) -> Bool in let name = newDevice.name!.lowercased() let deviceId = newDevice.deviceId! let search = searchText.lowercased() //return name.contains(search) == true return deviceId.contains(search) == true || name.contains(search) == true }))! } fillterResult = fillterDevicesNS } func filterGateway(_ searchText:String) { let _devicelist = self.gatewayList let devId = searchText var fillterDevicesNS : [GatewayData] if devId == "" || _devicelist == nil || _devicelist?.count == 0 { //没有输入则为传入的内容 fillterDevicesNS = _devicelist! }else{ fillterDevicesNS = (_devicelist?.filter({ (newDevice) -> Bool in let name = newDevice.network_name!.lowercased() let deviceId = newDevice.gatewayId!.lowercased() let search = searchText.lowercased() return deviceId.contains(search) == true || name.contains(search) == true }))! } fillterGatewayResult = fillterDevicesNS } } extension IHEquipmentSearchView:UITableViewDelegate,UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.totalDeviceList?.count ?? 0 // if currentIndex == 0 { // if searchController?.isActive == true { // let devCount = self.fillterResult?.count ?? 0 // let gatewayListCount = self.fillterGatewayResult?.count ?? 0 // let totalCount = devCount + gatewayListCount // return totalCount // }else{ // let devCount = self.devDataList?.count ?? 0 // let gatewayListCount = self.gatewayList?.count ?? 0 // let totalCount = devCount + gatewayListCount // return totalCount // } // }else if currentIndex == 5{ // if searchController?.isActive == true { // return self.fillterGatewayResult?.count ?? 0 // }else{ // return gatewayList?.count ?? 0 // } // }else { // if searchController?.isActive == true { // return self.fillterResult?.count ?? 0 // }else{ // return devDataList?.count ?? 0 // } // } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! IHEquipmentListCell cell.selectionStyle = .none if searchSchoolAndHotelList?.count ?? 0 > 0 { cell.devData = totalDeviceList?[indexPath.row] as? DevData } if searchGatewayList?.count ?? 0 > 0{ cell.gateway = totalDeviceList?[indexPath.row] as? GatewayData } return cell // if currentIndex == 0 { // if searchController?.isActive == true { // if let filterNs = fillterResult { // if indexPath.row < filterNs.count { // let devData = filterNs[indexPath.row] // cell.devData = devData // }else{ // let devData = self.fillterGatewayResult![indexPath.row - filterNs.count] // cell.gateway = devData // } // } // }else{ // if let deviceList = self.devDataList { // if indexPath.row < deviceList.count { // let devData = self.devDataList![indexPath.row] // cell.devData = devData // }else{ // let devData = self.gatewayList![indexPath.row - deviceList.count] // cell.gateway = devData // } // } // } // // }else if currentIndex == 5{ // let devData = searchController?.isActive == true ? self.fillterGatewayResult![indexPath.row] : self.gatewayList![indexPath.row] // cell.gateway = devData // }else{ // let devData = searchController?.isActive == true ? self.fillterResult![indexPath.row] : self.devDataList![indexPath.row] // cell.devData = devData // } // // // return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 135 } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if searchSchoolAndHotelList?.count ?? 0 > 0 && searchSchoolAndHotelList!.count > indexPath.row{ //设备 - (学校,酒店的) let devData = self.totalDeviceList?[indexPath.row] as? DevData self.delegate?.equipmentListSelected(devData) return } if searchGatewayList?.count ?? 0 > 0{ //网关 let devData = totalDeviceList?[indexPath.row] as? GatewayData delegate?.equipmentListSelected(devData) } // if currentIndex == 0 { // if searchController?.isActive == true { // if let filterNs = fillterResult { // if indexPath.row < filterNs.count { // let devData = filterNs[indexPath.row] // if let delegate = self.delegate { // //delegate.equipmentListSelected(devData) // } // }else{ // let devData = self.fillterGatewayResult![indexPath.row - filterNs.count] // if let delegate = self.delegate { // //delegate.equipmentListSelected(devData) // } // } // } // }else{ // if let deviceList = self.devDataList { // if indexPath.row < deviceList.count { // let devData = self.devDataList![indexPath.row] // if let delegate = self.delegate { // //delegate.equipmentListSelected(devData) // } // }else{ // let devData = self.gatewayList![indexPath.row - deviceList.count] // if let delegate = self.delegate { // //delegate.equipmentListSelected(devData) // } // } // } // } // // }else if currentIndex == 5{ // let devData = searchController?.isActive == true ? self.fillterGatewayResult![indexPath.row] : self.gatewayList![indexPath.row] // if let delegate = self.delegate { // //delegate.equipmentListSelected(devData) // } // }else{ // let devData = searchController?.isActive == true ? self.fillterResult![indexPath.row] : self.devDataList![indexPath.row] // if let delegate = self.delegate { // //delegate.equipmentListSelected(devData) // } // } // // // // if searchController?.isActive == true { // self.searchController?.isActive = false // } } ///========== //iOS 11 将进入此方法 actions.performsFirstActionWithFullSwipe = NO 可以控制是否自动删除。 func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let deleteAction = UIContextualAction(style: .normal, title: "") { (action, sourceView, completionHandler) in completionHandler(true) log.debug("删除") //处理事件(设备,网关) //删除设备 if self.searchSchoolAndHotelList?.count ?? 0 > 0 && self.searchSchoolAndHotelList!.count > indexPath.row{ let devData = self.totalDeviceList![indexPath.row] as? DevData self.delegate?.equipmentListDeleteDevice(devData!.type!, devId: devData!.id!, segmentIndex: self.currentIndex ?? 0) return } //删除网关 if self.searchGatewayList?.count ?? 0 > 0{ let gateway = self.totalDeviceList![indexPath.row] as? GatewayData self.delegate?.gatewayDelegate(gateway!, segmentIndex: self.currentIndex ?? 0) } // if let delegate = self.delegate{ // let devData = self.devDataList![indexPath.row] // delegate.equipmentListDeleteDevice(devData.devType!, devId: devData.id!, segmentIndex: self.currentIndex ?? 0) // } } deleteAction.backgroundColor = UIColor(hexString: "#EBEFF2") let historyAction = UIContextualAction(style: .normal, title: "") { (action, sourceView, completionHandler) in completionHandler(true) //处理事件(设备历史,网关升级) //设备-历史 if self.searchSchoolAndHotelList?.count ?? 0 > 0 && self.searchSchoolAndHotelList!.count > indexPath.row{ let devData = self.totalDeviceList![indexPath.row] as? DevData self.delegate?.equipmentHistory(devData!, segmentIndex: 0) return } //网关-升级 if self.searchGatewayList?.count ?? 0 > 0{ let gateway = self.totalDeviceList![indexPath.row] as? GatewayData self.delegate?.gatewayUpdate(gateway!, segmentIndex: 0) } // if self.currentIndex == 0{ // if let devDataList = self.devDataList { // if indexPath.row < devDataList.count { // if let delegate = self.delegate{ // let data = self.devDataList![indexPath.row] // delegate.equipmentHistory(data, segmentIndex: self.currentIndex ?? 0) // } // }else{ // if let delegate = self.delegate { // delegate.gatewayUpdate(segmentIndex: self.currentIndex ?? 5) // } // } // } // }else if self.currentIndex == 5{ // if let delegate = self.delegate { // delegate.gatewayUpdate(segmentIndex: self.currentIndex ?? 5) // } // }else{ // if let delegate = self.delegate{ // let data = self.devDataList![indexPath.row] // delegate.equipmentHistory(data, segmentIndex: self.currentIndex ?? 0) // } // } } historyAction.backgroundColor = UIColor(hexString: "#0A82CF") let settingAction = UIContextualAction(style: .normal, title: "") { (action, sourceView, completionHandler) in completionHandler(true) //处理事件 log.debug("设置") //设备 if self.searchSchoolAndHotelList?.count ?? 0 > 0 && self.searchSchoolAndHotelList!.count > indexPath.row{ let devData = self.totalDeviceList![indexPath.row] as? DevData self.delegate?.equipmentSetting(devData!, segmentIndex: 0) return } //网关 if self.searchGatewayList?.count ?? 0 > 0{ let gateway = self.totalDeviceList![indexPath.row] as? GatewayData self.delegate?.gatewaySetting(gateway!, segmentIndex: 0) } // if let delegate = self.delegate{ // let data = self.devDataList![indexPath.row] // delegate.equipmentSetting(data, segmentIndex: self.currentIndex ?? 0) // } } // #05CFAB settingAction.backgroundColor = UIColor(hexString: "#573F95") let actions = UISwipeActionsConfiguration(actions: [settingAction,historyAction,deleteAction]) actions.performsFirstActionWithFullSwipe = false return actions } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { } func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true } func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) { editingIndexPath = indexPath log.debug("willBeginEditingRowAt") setNeedsLayout() // 触发layoutSubviews() } //取消选中 func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) { editingIndexPath = nil log.debug("didEndEditingRowAt") } func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { log.debug("canMoveRowAt") return true } func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { log.debug("sourceIndexPath - \(sourceIndexPath) --destinationIndexPath \(destinationIndexPath)") } func configSwipeButtons() { // log.debug("重新绘制 ") //iOS 11 层级 UITableView -> UISwipActionPull var swipeButtons : [UIView]? if #available(iOS 11, *) { swipeButtons = self.tableView.getSwipeButtonView() }else{ // iOS 8-10: 画UITableView->UITableViewCell->UITableVIewCellDeleteConfirmationView let tabCell = self.tableView.cellForRow(at: self.editingIndexPath!) swipeButtons = tabCell?.getSwipeButtonView() } // log.debug("子控件 -- \(swipeButtons)") if swipeButtons?.count == 0 || swipeButtons == nil{ return } for i in 0..<(swipeButtons?.count)!{ let button = swipeButtons![i] as! UIButton if i == 0{ configSwipeButton(button, backgroundImage: UIImage(named: "ic_delete_bg")!) }else if i == 1{ //第二按钮(历史,更新固件) let data = self.totalDeviceList![editingIndexPath!.row] as? DevData if data != nil{ //设备的 configSwipeButton(button, backgroundImage: UIImage(named: "ic_hitory_bg")!) }else{ //网关的 configSwipeButton(button, backgroundImage: UIImage(named: "ic_update")!) } }else{ //第三个(设置) configSwipeButton(button, backgroundImage: UIImage(named: "ic_setting_bg")!) } // let button = swipeButtons![i] as! UIButton // if i == 0 { // configSwipeButton(button, backgroundImage: UIImage(named: "ic_delete_bg")!) // }else if i == 1{ // // if currentIndex == 0 { // if self.searchController?.isActive == true { // if let devDataList = self.fillterResult { // if self.editingIndexPath!.row < devDataList.count { // configSwipeButton(button, backgroundImage: UIImage(named: "ic_hitory_bg")!) // }else{ // configSwipeButton(button, backgroundImage: UIImage(named: "ic_update")!) // } // } // }else{ // if let devDataList = self.devDataList { // if self.editingIndexPath!.row < devDataList.count { // configSwipeButton(button, backgroundImage: UIImage(named: "ic_hitory_bg")!) // }else{ // configSwipeButton(button, backgroundImage: UIImage(named: "ic_update")!) // } // } // } // // }else if currentIndex == 5{ // configSwipeButton(button, backgroundImage: UIImage(named: "ic_update")!) // }else { // configSwipeButton(button, backgroundImage: UIImage(named: "ic_hitory_bg")!) // } // }else{ // configSwipeButton(button, backgroundImage: UIImage(named: "ic_setting_bg")!) // } } } func configSwipeButton(_ button: UIButton,backgroundImage:UIImage) { button.layer.cornerRadius = 5 button.layer.masksToBounds = true button.setBackgroundImage(backgroundImage, for: .normal) button.setBackgroundImage(backgroundImage, for: .selected) button.titleLabel?.font = UIFont(name: PingFangSC_Medium, size: 11) let frame = button.frame button.frame = CGRect(x: frame.origin.x + 5 , y: frame.origin.y + 10, width: frame.size.width - 10, height: frame.size.height - 20) } } extension IHEquipmentSearchView : UISearchResultsUpdating,UISearchBarDelegate,UISearchControllerDelegate{ func updateSearchResults(for searchController: UISearchController) { if let searchText = searchController.searchBar.text { //filterContent(for: searchText) if searchText != ""{ self.searchText = searchText delegate?.searchDevice(searchText) if isRemove == false{ tableViewRefresh() } isRemove = true } } } func didPresentSearchController(_ searchController: UISearchController) { log.debug("self.tableView.frame - \(self.tableView.frame)") showCancelButtonWhileEditing = true } func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { //删除上下拉刷新01 isRemove = false tableView.es.removeRefreshHeader() tableView.es.removeRefreshFooter() } func willDismissSearchController(_ searchController: UISearchController) { log.debug("self.tableView.frame - \(self.tableView.frame)") showCancelButtonWhileEditing = false } }