IHIHRoomListCell.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // IHIHRoomListCell.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/14.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. //import TKSwitcherCollection
  10. protocol IHRoomListCellDelegate : NSObjectProtocol {
  11. func oprationSwitch(_ id: String, devType:String,status:String,currentIndex:IndexPath)
  12. }
  13. class IHIHRoomListCell: UICollectionViewCell {
  14. weak var delegate : IHRoomListCellDelegate?
  15. var currentIndexPath :IndexPath?
  16. var model:DevData?{
  17. didSet{
  18. if let model = self.model {
  19. if let devId = model.id,
  20. let status = model.status,
  21. let alarmStatus = model.alarmStatus ,
  22. let name = model.name,
  23. let devType = model.devType,
  24. let level = model.level,
  25. let light = model.light,
  26. let color = model.color,
  27. let online = model.online
  28. {
  29. self.devId = devId
  30. self.devType = devType
  31. self.devNameLabel.text = name
  32. if alarmStatus == "1" { //故障有
  33. self.bgView.image = UIImage(named: "故障状态")
  34. // self.switchBtn.setImage(UIImage(named: "offSwitch"), for: .normal)
  35. self.switchBtn.isSelected = true
  36. self.switchBtn.setBackgroundImage(UIImage(named: "启用状态-1"), for: .normal)
  37. }else if alarmStatus == "0"{ //故障无
  38. if status == "1" { //开
  39. setOnlineStatus()
  40. self.bgView.image = UIImage(named: "启用状态")
  41. }else if status == "0"{//关
  42. setOfflineStaus()
  43. self.bgView.image = UIImage(named: "关闭状态")
  44. }
  45. }
  46. if devType == "1"{
  47. self.devTypeLabel.text = "灯控"
  48. self.switchBtn.isHidden = false
  49. self.lightnessLabel.text = light + "%"
  50. }else if devType == "2"{
  51. self.devTypeLabel.text = "HCL灯控"
  52. self.switchBtn.isHidden = false
  53. self.lightnessLabel.text = light + "%"
  54. }else if devType == "3"{
  55. self.devTypeLabel.text = "感应器"
  56. self.switchBtn.isHidden = true
  57. if online == "0"{
  58. self.lightnessLabel.text = "离线"
  59. }else if online == "1"{
  60. self.lightnessLabel.text = "在线"
  61. }
  62. }else if devType == "4"{
  63. self.devTypeLabel.text = "空气净化器"
  64. self.switchBtn.isHidden = false
  65. var airLevel = ""
  66. if level == "1" {
  67. airLevel = "低档"
  68. }else if level == "2"{
  69. airLevel = "中档"
  70. }else if level == "3"{
  71. airLevel = "高档"
  72. }
  73. self.lightnessLabel.text = airLevel
  74. }
  75. }
  76. }
  77. }
  78. }
  79. private var devId :String?
  80. private var devType:String? = "1"
  81. @IBOutlet weak var bgView: UIImageView!
  82. @IBOutlet weak var repairIcon: UIImageView!
  83. @IBOutlet weak var devNameLabel: UILabel!
  84. @IBOutlet weak var devTypeLabel: UILabel!
  85. @IBOutlet weak var lightnessLabel: UILabel!
  86. // @IBOutlet weak var exchangeSwitch: TKExchangeSwitch!
  87. @IBOutlet weak var switchBtn: UIButton!
  88. override func awakeFromNib() {
  89. super.awakeFromNib()
  90. self.switchBtn.setBackgroundImage(UIImage(named: "onSwitch"), for: .selected)
  91. self.switchBtn.setBackgroundImage(UIImage(named: "offSwitch"), for: .normal)
  92. switchBtn.addTarget(self, action: #selector(switchAction(sender:)), for: .touchUpInside)
  93. }
  94. func setOnlineStatus() {
  95. self.switchBtn.isSelected = true
  96. self.switchBtn.setBackgroundImage(UIImage(named: "onSwitch"), for: .selected)
  97. self.devNameLabel.textColor = UIColor(hexString: "#FFFFFF")
  98. self.devTypeLabel.textColor = UIColor(hexString: "#FFFFFF")
  99. self.lightnessLabel.textColor = UIColor(hexString: "#FFFFFF")
  100. }
  101. func setOfflineStaus() {
  102. self.switchBtn.setBackgroundImage(UIImage(named: "offSwitch"), for: .normal)
  103. self.switchBtn.isSelected = false
  104. self.devNameLabel.textColor = UIColor(hexString: "#333333")
  105. self.devTypeLabel.textColor = UIColor(hexString: "#333333")
  106. self.lightnessLabel.textColor = UIColor(hexString: "#333333")
  107. }
  108. @objc func switchAction(sender:UIButton) {
  109. sender.isSelected = !sender.isSelected
  110. let status = sender.isSelected == true ? "1" : "0"
  111. if let delegate = self.delegate {
  112. delegate.oprationSwitch(self.devId!, devType: self.devType!, status: status, currentIndex: self.currentIndexPath!)
  113. }
  114. }
  115. }