IHAirQualityView.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // IHAirQualityView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/12.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import JXPagingView
  10. import TKSwitcherCollection
  11. let kNotificationIHAirQualityViewReloadAirData = "kNotificationIHAirQualityViewReloadAirData"
  12. class IHAirQualityView: UIView ,UIScrollViewDelegate{
  13. //环境信息
  14. var envorenmentInfo : Home_room_env_status?{
  15. didSet{
  16. if self.envorenmentInfo != nil {
  17. self.tableView.reloadData()
  18. }
  19. }
  20. }
  21. static let switchCellId = "switchCellId"
  22. static let levelCellId = "levelCellId"
  23. static let subHeaderCellId = "subHeaderCellId"
  24. static let normalCellId = "normalCellId"
  25. static let weeklyCellId = "weeklyCellId"
  26. static let chartsCellId = "chartsCellId" //
  27. lazy var tableView: UITableView = {
  28. let tableView = UITableView(frame:self.bounds, style: .plain)
  29. tableView.dataSource = self
  30. tableView.delegate = self
  31. return tableView
  32. }()
  33. var listViewDidScrollCallback: ((UIScrollView) -> ())?
  34. override init(frame: CGRect) {
  35. super.init(frame: frame)
  36. addSubview(tableView)
  37. self.backgroundColor = UIColor.white
  38. self.tableView.backgroundColor = UIColor.white
  39. tableView.separatorStyle = .none
  40. tableView.register(UINib(nibName: "IHAQSwitchCell", bundle: nil), forCellReuseIdentifier: IHAirQualityView.switchCellId)
  41. tableView.register(UINib(nibName: "IHAQLevelCell", bundle: nil), forCellReuseIdentifier: IHAirQualityView.levelCellId)
  42. tableView.register(UINib(nibName: "IHAQSubHeaderCell", bundle: nil), forCellReuseIdentifier: IHAirQualityView.subHeaderCellId)
  43. tableView.register(UINib(nibName: "IHAQNormalCell", bundle: nil), forCellReuseIdentifier: IHAirQualityView.normalCellId)
  44. tableView.register(UINib(nibName: "IHWeeklyCell", bundle: nil), forCellReuseIdentifier: IHAirQualityView.weeklyCellId)
  45. tableView.register(UINib(nibName: "IHQualityChartsCell", bundle: nil), forCellReuseIdentifier: IHAirQualityView.chartsCellId)
  46. NotificationCenter.default.addObserver(self, selector: #selector(reloadAirData(_:)), name: NSNotification.Name(kNotificationIHAirQualityViewReloadAirData), object: nil)
  47. }
  48. required init?(coder: NSCoder) {
  49. fatalError("init(coder:) has not been implemented")
  50. }
  51. @objc func reloadAirData(_ notify:Notification) {
  52. let result = notify.object as! Home_room_env_status
  53. self.envorenmentInfo = result
  54. }
  55. override func layoutSubviews() {
  56. super.layoutSubviews()
  57. tableView.frame = self.bounds
  58. }
  59. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  60. self.listViewDidScrollCallback?(scrollView)
  61. }
  62. }
  63. extension IHAirQualityView:UITableViewDelegate,UITableViewDataSource{
  64. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  65. return 4
  66. }
  67. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  68. if indexPath.row == 0{
  69. let cell = tableView.dequeueReusableCell(withIdentifier: IHAirQualityView.levelCellId) as! IHAQLevelCell
  70. cell.selectionStyle = .none
  71. return cell
  72. }else if indexPath.row == 1 {
  73. let cell = tableView.dequeueReusableCell(withIdentifier: IHAirQualityView.subHeaderCellId) as! IHAQSubHeaderCell
  74. cell.selectionStyle = .none
  75. return cell
  76. }else if indexPath.row == 2{
  77. let cell = tableView.dequeueReusableCell(withIdentifier: IHAirQualityView.chartsCellId) as! IHQualityChartsCell
  78. cell.averageAir = self.envorenmentInfo?.averageAir
  79. cell.dataList = self.envorenmentInfo?.dataList
  80. cell.timeList = self.envorenmentInfo?.timeList
  81. cell.selectionStyle = .none
  82. return cell
  83. }else {
  84. let cell = tableView.dequeueReusableCell(withIdentifier: IHAirQualityView.weeklyCellId) as! IHWeeklyCell
  85. cell.selectionStyle = .none
  86. cell.humidity = self.envorenmentInfo?.humidity
  87. cell.temperature = self.envorenmentInfo?.temperature
  88. return cell
  89. }
  90. }
  91. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  92. if indexPath.row == 0{
  93. return 100
  94. }else if indexPath.row == 1 {
  95. return 50
  96. }else if indexPath.row == 2{
  97. return 147
  98. }else {
  99. return 134
  100. }
  101. }
  102. }
  103. extension IHAirQualityView:JXPagingViewListViewDelegate{
  104. func listView() -> UIView {
  105. return self
  106. }
  107. func listScrollView() -> UIScrollView {
  108. return self.tableView
  109. }
  110. func listViewDidScrollCallback(callback: @escaping (UIScrollView) -> ()) {
  111. self.listViewDidScrollCallback = callback
  112. }
  113. }