IHSensorFirstCell.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // IHSensorFirstCell.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/17.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. class IHSensorFirstCell: UITableViewCell {
  10. var sensorData:SensorInfodata?{
  11. didSet{
  12. if let sensorData = self.sensorData {
  13. if let formaldehyde = sensorData.formaldehyde,
  14. let pm25 = sensorData.pm25, //pm2.5
  15. let temperature = sensorData.temperature,
  16. let humidity = sensorData.humidity,
  17. let co2 = sensorData.co2,
  18. let illuminance = sensorData.illuminance, //废弃使用
  19. let noise = sensorData.noise, //废弃使用
  20. let voc = sensorData.voc,
  21. let name = sensorData.name,
  22. let deviceId = sensorData.deviceId,
  23. let useTime = sensorData.useTime,
  24. let alarmStatus = sensorData.alarmStatus,
  25. let alarmInfo = sensorData.alarmInfo{
  26. self.sensorNameLabel.text = name
  27. self.sensorIdLabel.text = "感应器 · " + deviceId
  28. self.useTimeLabel.text = useTime + "h"
  29. var _values = [NSAttributedString]()
  30. _values.append(setAttrStr(temperature + "℃", length: 0) )
  31. _values.append(setAttrStr(humidity + "%RH", length: 0) )
  32. _values.append(setAttrStr(co2 + "ppm", length: 0) )
  33. _values.append(setAttrStr(pm25 + "ug/m3", length: 1) )
  34. _values.append(setAttrStr(voc + "ppb", length: 0) )
  35. _values.append(setAttrStr(formaldehyde + "ppb", length: 0) )
  36. self.values = _values
  37. if alarmStatus == "0" {//正常状态
  38. self.alarmBox.isHidden = true
  39. }else if alarmStatus == "1"{ //故障状态
  40. self.alarmBox.isHidden = false
  41. self.alarmInfoLabel.text = alarmInfo
  42. }
  43. }
  44. }
  45. }
  46. }
  47. let titles = ["温度","湿度","CO2","PM2.5","TVOC","HCHO"]
  48. //
  49. var values : [NSAttributedString]? {
  50. didSet{
  51. self.collectionView.reloadData()
  52. }
  53. }
  54. @IBOutlet weak var collectionBox: UIView!
  55. @IBOutlet weak var sensorImageV: UIImageView!
  56. @IBOutlet weak var sensorNameLabel: UILabel!
  57. @IBOutlet weak var sensorIdLabel: UILabel!
  58. @IBOutlet weak var useTimeLabel: UILabel!
  59. @IBOutlet weak var alarmInfoLabel: UILabel!
  60. @IBOutlet weak var alarmBox: UIView!
  61. lazy var collectionView: UICollectionView = {
  62. let layout = UICollectionViewFlowLayout()
  63. let cellwidth = (KSCREENWIDTH - 40) / 3
  64. let cellheight : CGFloat = 183 / 3
  65. layout.itemSize = CGSize(width: cellwidth, height: cellheight)//设置cell的大小
  66. layout.minimumLineSpacing = 0
  67. layout.minimumInteritemSpacing = 0 //同一列最小间隔
  68. layout.scrollDirection = .vertical //纵向滚动
  69. layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)//设置内边距
  70. let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH - 40 , height: 122), collectionViewLayout: layout)
  71. collectionView.backgroundColor = UIColor.clear
  72. collectionView.delegate = self
  73. collectionView.dataSource = self
  74. collectionView.showsHorizontalScrollIndicator = false
  75. collectionView.showsVerticalScrollIndicator = false
  76. collectionView.register(UINib(nibName: "IHSensorItemCell", bundle: nil), forCellWithReuseIdentifier: "cell")
  77. return collectionView
  78. }()
  79. override func awakeFromNib() {
  80. super.awakeFromNib()
  81. // Initialization code
  82. self.collectionBox.addSubview(collectionView)
  83. }
  84. override func setSelected(_ selected: Bool, animated: Bool) {
  85. super.setSelected(selected, animated: animated)
  86. // Configure the view for the selected state
  87. }
  88. //lenght 表示富文本的长读 ug/m³ 处理平方或者立方
  89. private func setAttrStr(_ str: String, length:Int) -> NSMutableAttributedString {
  90. let bigFont = UIFont(name: PingFangSC_Semibold, size: 16)
  91. let smallFont = UIFont(name: PingFangSC_Semibold, size: 9)
  92. let attrString = NSMutableAttributedString(string: str, attributes: [.font: bigFont as Any])
  93. if length == 1 {
  94. attrString.setAttributes([.font: smallFont as Any, .baselineOffset: 5], range: NSRange(location: str.count - length, length: length))
  95. }
  96. return attrString
  97. }
  98. }
  99. extension IHSensorFirstCell :UICollectionViewDelegate,UICollectionViewDataSource{
  100. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  101. return values?.count ?? 0
  102. }
  103. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  104. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? IHSensorItemCell
  105. cell?.mTitle = titles[indexPath.row]
  106. cell?.mValue = values?[indexPath.row]
  107. // if indexPath.row == 2 {
  108. // cell?.isAlarm = true
  109. // }else{
  110. // cell?.isAlarm = false
  111. // }
  112. return cell!
  113. }
  114. }