IHCurtainView.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // IHCurtainView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/7/25.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import PKHUD
  10. protocol CurtainViewDelegate : NSObjectProtocol{
  11. //设置窗帘
  12. func setCurtain(dataLists : [[String : String]])
  13. }
  14. class IHCurtainView: UIView {
  15. //var dataSource = [["Full-on mode" : "On"],["Full-off mode" : "Off"],["Attend class mode" : "On"],["Finish class mode" : "Off"], ["Noon break mode" : "Off"],["Self-study mode" : "On"],["Blackboard mode" : "On"],["Projection mode" : "Off"]]
  16. var tableView : UITableView?
  17. weak var delegate : CurtainViewDelegate?
  18. private var dataSources = [[String : String]]()
  19. var curtainModelData : CurtainModel?{
  20. didSet{
  21. let data1 = ["全开模式" : curtainModelData?.open_status == "0" ? "关" : "开"]
  22. let data2 = ["全关模式" : curtainModelData?.close_status == "0" ? "关" : "开"]
  23. let data3 = ["上课模式" : curtainModelData?.att_class_status == "0" ? "关" : "开"]
  24. let data4 = ["下课模式" : curtainModelData?.fin_class_status == "0" ? "关" : "开"]
  25. let data5 = ["午休模式" : curtainModelData?.break_status == "0" ? "关" : "开"]
  26. let data6 = ["自习模式" : curtainModelData?.self_stu_status == "0" ? "关" : "开"]
  27. let data7 = ["板书模式" : curtainModelData?.blackboard_status == "0" ? "关" : "开"]
  28. let data8 = ["投影模式" : curtainModelData?.shadow_status == "0" ? "关" : "开"]
  29. dataSources.append(data1)
  30. dataSources.append(data2)
  31. dataSources.append(data3)
  32. dataSources.append(data4)
  33. dataSources.append(data5)
  34. dataSources.append(data6)
  35. dataSources.append(data7)
  36. dataSources.append(data8)
  37. self.tableView?.reloadData()
  38. }
  39. }
  40. override init(frame: CGRect) {
  41. super.init(frame: frame)
  42. self.createUI()
  43. NotificationCenter.default.addObserver(self, selector: #selector(acceptCurtainChoseData), name: NSNotification.Name(KNotificationCurtain), object: nil)
  44. }
  45. @objc func acceptCurtainChoseData(noti : Notification){
  46. let dic = noti.userInfo
  47. let indexPath = dic?["indexPath"] as! IndexPath
  48. let choseValue = dic?["choseValue"] as! String
  49. var data = self.dataSources[indexPath.row]
  50. let key = data.keys.first!
  51. data.updateValue(choseValue, forKey: key)
  52. //更新数组
  53. self.dataSources[indexPath.row] = data
  54. self.tableView?.reloadRows(at: [indexPath], with: .automatic)
  55. }
  56. deinit {
  57. NotificationCenter.default.removeObserver(self)
  58. }
  59. required init?(coder: NSCoder) {
  60. fatalError("init(coder:) has not been implemented")
  61. }
  62. private func createUI() {
  63. if KNavBarHeight == 64 {
  64. self.tableView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT - KNavBarHeight - 70), style: .plain)
  65. }else {
  66. self.tableView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT - KNavBarHeight - 34 - 70), style: .plain)
  67. }
  68. tableView?.dataSource = self
  69. tableView?.delegate = self
  70. tableView?.separatorStyle = .none
  71. tableView?.rowHeight = 120.0
  72. //self.tableView = tableView
  73. self.addSubview(tableView!)
  74. let between : CGFloat = 30.0
  75. let saveBtn = UIButton.init(frame: CGRect.init(x: between, y: tableView!.size.height + 20.0, width: KSCREENWIDTH - between * 2, height: 40))
  76. saveBtn.setTitle("保存", for: .normal)
  77. saveBtn.backgroundColor = UIColor.init(hexString: "#573F95")
  78. saveBtn.addTarget(self, action: #selector(saveValueBtn), for: .touchUpInside)
  79. saveBtn.layer.cornerRadius = 5
  80. saveBtn.layer.masksToBounds = true
  81. self.addSubview(saveBtn)
  82. }
  83. }
  84. extension IHCurtainView {
  85. @objc func saveValueBtn(sender : UIButton){
  86. // HUD.flash(.label("加载中..."), delay: 8)
  87. HUD.flash(.progress, delay: 8.0)
  88. self.delegate?.setCurtain(dataLists: dataSources)
  89. }
  90. }
  91. extension IHCurtainView : UITableViewDelegate,UITableViewDataSource{
  92. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  93. return self.dataSources.count
  94. }
  95. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  96. let cell = IHCurtainCell.curtainCellWithTableViewAndIndexPath(tableView: tableView, indexPath: indexPath)
  97. cell.indexPath = indexPath
  98. cell.dataSource = self.dataSources[indexPath.row]
  99. return cell
  100. }
  101. }