IHAssociatedView.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // IHAssociatedView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/4/10.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import JXSegmentedView
  10. protocol IHAssociatedViewDelegate : NSObjectProtocol{
  11. func emptyViewSearchDataSegment(_ index:Int)
  12. func deviceConnectGateway(_ deviceId:String)
  13. func deviceDisconteGateway(_ deviceId:String)
  14. func controlightOn(_ isTurnON:Bool,lampId:String)
  15. }
  16. class IHAssociatedView: UIView {
  17. weak var delegate:IHAssociatedViewDelegate?
  18. var currentIndex:Int = 0
  19. var associatedList:[GatewayLamp]?{
  20. didSet{
  21. if let associatedList = self.associatedList{
  22. if(associatedList.count > 0){
  23. tableView.ly_emptyView?.isHidden = true
  24. self.tableView.reloadData()
  25. }
  26. }
  27. }
  28. }
  29. var notAssociatedList:[GatewayLamp]?{
  30. didSet{
  31. if let notAssociatedList = self.notAssociatedList{
  32. if(notAssociatedList.count > 0){
  33. tableView.ly_emptyView?.isHidden = true
  34. self.tableView.reloadData()
  35. }
  36. }
  37. }
  38. }
  39. var emptyView:IHEmptyView?
  40. lazy var tableView: UITableView = {
  41. let tableView = UITableView(frame: .zero, style: .plain)
  42. tableView.delegate = self
  43. tableView.dataSource = self
  44. tableView.allowsSelection = true
  45. tableView.separatorStyle = .none
  46. tableView.register(UINib(nibName: "IHAssociatedCell", bundle: nil), forCellReuseIdentifier: "associated")
  47. tableView.register(UINib(nibName: "IHNotAssociatedCell", bundle: nil), forCellReuseIdentifier: "notassociated")
  48. return tableView
  49. }()
  50. override init(frame: CGRect) {
  51. super.init(frame: frame)
  52. createUI()
  53. }
  54. required init?(coder: NSCoder) {
  55. fatalError("init(coder:) has not been implemented")
  56. }
  57. override func layoutSubviews() {
  58. super.layoutSubviews()
  59. tableView.frame = self.bounds
  60. emptyView?.frame = self.bounds
  61. }
  62. func createUI() {
  63. addSubview(tableView)
  64. emptyView = Bundle.main.loadNibNamed("IHEmptyView", owner: nil, options: nil)?.last as? IHEmptyView
  65. emptyView?.searchCallback = { [unowned self] in
  66. //搜索事件
  67. if let delegate = self.delegate {
  68. delegate.emptyViewSearchDataSegment(self.currentIndex)
  69. }
  70. }
  71. // 空数据展示界面
  72. let emptyV :HDEmptyView = HDEmptyView.emptyViewWithCustomView(customView: emptyView!) as! HDEmptyView
  73. tableView.ly_emptyView = emptyV
  74. tableView.ly_showEmptyView()
  75. }
  76. }
  77. extension IHAssociatedView :JXSegmentedListContainerViewListDelegate{
  78. func listView() -> UIView {
  79. return self
  80. }
  81. }
  82. extension IHAssociatedView:UITableViewDelegate,UITableViewDataSource{
  83. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  84. if currentIndex == 0 {
  85. return self.associatedList?.count ?? 0
  86. }else{
  87. return self.notAssociatedList?.count ?? 0
  88. }
  89. }
  90. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  91. return 60
  92. }
  93. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  94. if currentIndex == 0 {
  95. let cell = tableView.dequeueReusableCell(withIdentifier: "associated") as! IHAssociatedCell
  96. let gateDevice = self.associatedList![indexPath.row]
  97. cell.gateDevice = gateDevice
  98. cell.selectionStyle = .none
  99. cell.disconnectCallback = { [unowned self] in
  100. if let delegate = self.delegate {
  101. delegate.deviceDisconteGateway(gateDevice.deviceId!)
  102. }
  103. }
  104. cell.turnonCallback = {[unowned self] (isTurnOn) in
  105. if let delegate = self.delegate {
  106. if gateDevice.type != "7"{
  107. //除了面板是不可以开关的
  108. delegate.controlightOn(isTurnOn, lampId: gateDevice.id!)
  109. }
  110. }
  111. }
  112. return cell
  113. }else{
  114. let cell = tableView.dequeueReusableCell(withIdentifier: "notassociated") as! IHNotAssociatedCell
  115. cell.selectionStyle = .none
  116. let gateDevice = self.notAssociatedList![indexPath.row]
  117. cell.gateDevice = self.notAssociatedList![indexPath.row]
  118. cell.connectCallback = { [unowned self] in
  119. if let delegate = self.delegate {
  120. delegate.deviceConnectGateway(gateDevice.deviceId!)
  121. }
  122. }
  123. return cell
  124. }
  125. }
  126. }