// // IHSensorFirstCell.swift // Inhealth // // Created by weclouds on 2019/12/17. // Copyright © 2019 weclouds. All rights reserved. // import UIKit class IHSensorFirstCell: UITableViewCell { var sensorData:SensorInfodata?{ didSet{ if let sensorData = self.sensorData { if let formaldehyde = sensorData.formaldehyde, let pm25 = sensorData.pm25, //pm2.5 let temperature = sensorData.temperature, let humidity = sensorData.humidity, let co2 = sensorData.co2, let illuminance = sensorData.illuminance, //废弃使用 let noise = sensorData.noise, //废弃使用 let voc = sensorData.voc, let name = sensorData.name, let deviceId = sensorData.deviceId, let useTime = sensorData.useTime, let alarmStatus = sensorData.alarmStatus, let alarmInfo = sensorData.alarmInfo{ self.sensorNameLabel.text = name self.sensorIdLabel.text = "感应器 · " + deviceId self.useTimeLabel.text = useTime + "h" var _values = [NSAttributedString]() _values.append(setAttrStr(temperature + "℃", length: 0) ) _values.append(setAttrStr(humidity + "%RH", length: 0) ) _values.append(setAttrStr(co2 + "ppm", length: 0) ) _values.append(setAttrStr(pm25 + "ug/m3", length: 1) ) _values.append(setAttrStr(voc + "ppb", length: 0) ) _values.append(setAttrStr(formaldehyde + "ppb", length: 0) ) self.values = _values if alarmStatus == "0" {//正常状态 self.alarmBox.isHidden = true }else if alarmStatus == "1"{ //故障状态 self.alarmBox.isHidden = false self.alarmInfoLabel.text = alarmInfo } } } } } let titles = ["温度","湿度","CO2","PM2.5","TVOC","HCHO"] // var values : [NSAttributedString]? { didSet{ self.collectionView.reloadData() } } @IBOutlet weak var collectionBox: UIView! @IBOutlet weak var sensorImageV: UIImageView! @IBOutlet weak var sensorNameLabel: UILabel! @IBOutlet weak var sensorIdLabel: UILabel! @IBOutlet weak var useTimeLabel: UILabel! @IBOutlet weak var alarmInfoLabel: UILabel! @IBOutlet weak var alarmBox: UIView! lazy var collectionView: UICollectionView = { let layout = UICollectionViewFlowLayout() let cellwidth = (KSCREENWIDTH - 40) / 3 let cellheight : CGFloat = 183 / 3 layout.itemSize = CGSize(width: cellwidth, height: cellheight)//设置cell的大小 layout.minimumLineSpacing = 0 layout.minimumInteritemSpacing = 0 //同一列最小间隔 layout.scrollDirection = .vertical //纵向滚动 layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)//设置内边距 let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH - 40 , height: 122), collectionViewLayout: layout) collectionView.backgroundColor = UIColor.clear collectionView.delegate = self collectionView.dataSource = self collectionView.showsHorizontalScrollIndicator = false collectionView.showsVerticalScrollIndicator = false collectionView.register(UINib(nibName: "IHSensorItemCell", bundle: nil), forCellWithReuseIdentifier: "cell") return collectionView }() override func awakeFromNib() { super.awakeFromNib() // Initialization code self.collectionBox.addSubview(collectionView) } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } //lenght 表示富文本的长读 ug/m³ 处理平方或者立方 private func setAttrStr(_ str: String, length:Int) -> NSMutableAttributedString { let bigFont = UIFont(name: PingFangSC_Semibold, size: 16) let smallFont = UIFont(name: PingFangSC_Semibold, size: 9) let attrString = NSMutableAttributedString(string: str, attributes: [.font: bigFont as Any]) if length == 1 { attrString.setAttributes([.font: smallFont as Any, .baselineOffset: 5], range: NSRange(location: str.count - length, length: length)) } return attrString } } extension IHSensorFirstCell :UICollectionViewDelegate,UICollectionViewDataSource{ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return values?.count ?? 0 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? IHSensorItemCell cell?.mTitle = titles[indexPath.row] cell?.mValue = values?[indexPath.row] // if indexPath.row == 2 { // cell?.isAlarm = true // }else{ // cell?.isAlarm = false // } return cell! } }