// // IHAssociatedView.swift // Inhealth // // Created by weclouds on 2020/4/10. // Copyright © 2020 weclouds. All rights reserved. // import UIKit import JXSegmentedView protocol IHAssociatedViewDelegate : NSObjectProtocol{ func emptyViewSearchDataSegment(_ index:Int) func deviceConnectGateway(_ deviceId:String) func deviceDisconteGateway(_ deviceId:String) func controlightOn(_ isTurnON:Bool,lampId:String) } class IHAssociatedView: UIView { weak var delegate:IHAssociatedViewDelegate? var currentIndex:Int = 0 var associatedList:[GatewayLamp]?{ didSet{ if let associatedList = self.associatedList{ if(associatedList.count > 0){ tableView.ly_emptyView?.isHidden = true self.tableView.reloadData() } } } } var notAssociatedList:[GatewayLamp]?{ didSet{ if let notAssociatedList = self.notAssociatedList{ if(notAssociatedList.count > 0){ tableView.ly_emptyView?.isHidden = true self.tableView.reloadData() } } } } var emptyView:IHEmptyView? 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: "IHAssociatedCell", bundle: nil), forCellReuseIdentifier: "associated") tableView.register(UINib(nibName: "IHNotAssociatedCell", bundle: nil), forCellReuseIdentifier: "notassociated") return tableView }() override init(frame: CGRect) { super.init(frame: frame) createUI() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func layoutSubviews() { super.layoutSubviews() tableView.frame = self.bounds emptyView?.frame = self.bounds } func createUI() { addSubview(tableView) emptyView = Bundle.main.loadNibNamed("IHEmptyView", owner: nil, options: nil)?.last as? IHEmptyView emptyView?.searchCallback = { [unowned self] in //搜索事件 if let delegate = self.delegate { delegate.emptyViewSearchDataSegment(self.currentIndex) } } // 空数据展示界面 let emptyV :HDEmptyView = HDEmptyView.emptyViewWithCustomView(customView: emptyView!) as! HDEmptyView tableView.ly_emptyView = emptyV tableView.ly_showEmptyView() } } extension IHAssociatedView :JXSegmentedListContainerViewListDelegate{ func listView() -> UIView { return self } } extension IHAssociatedView:UITableViewDelegate,UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if currentIndex == 0 { return self.associatedList?.count ?? 0 }else{ return self.notAssociatedList?.count ?? 0 } } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 60 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if currentIndex == 0 { let cell = tableView.dequeueReusableCell(withIdentifier: "associated") as! IHAssociatedCell let gateDevice = self.associatedList![indexPath.row] cell.gateDevice = gateDevice cell.selectionStyle = .none cell.disconnectCallback = { [unowned self] in if let delegate = self.delegate { delegate.deviceDisconteGateway(gateDevice.deviceId!) } } cell.turnonCallback = {[unowned self] (isTurnOn) in if let delegate = self.delegate { if gateDevice.type != "7"{ //除了面板是不可以开关的 delegate.controlightOn(isTurnOn, lampId: gateDevice.id!) } } } return cell }else{ let cell = tableView.dequeueReusableCell(withIdentifier: "notassociated") as! IHNotAssociatedCell cell.selectionStyle = .none let gateDevice = self.notAssociatedList![indexPath.row] cell.gateDevice = self.notAssociatedList![indexPath.row] cell.connectCallback = { [unowned self] in if let delegate = self.delegate { delegate.deviceConnectGateway(gateDevice.deviceId!) } } return cell } } }