IHGatewayListView.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // IHGatewayListView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/4/7.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. protocol IHGatewayListViewDelegate {
  10. // func (<#parameters#>)
  11. }
  12. class IHGatewayListView: UIView {
  13. var devDataList:[GatewayData]?{
  14. didSet{
  15. self.tableView.reloadData()
  16. }
  17. }
  18. //编辑的cell
  19. var editingIndexPath : IndexPath?
  20. lazy var tableView: UITableView = {
  21. let tableView = UITableView(frame: .zero, style: .plain)
  22. tableView.delegate = self
  23. tableView.dataSource = self
  24. tableView.allowsSelection = true
  25. tableView.separatorStyle = .none
  26. tableView.register(UINib(nibName: "IHEquipmentListCell", bundle: nil), forCellReuseIdentifier: "cell")
  27. return tableView
  28. }()
  29. override init(frame: CGRect) {
  30. super.init(frame: frame)
  31. addSubview(tableView)
  32. }
  33. required init?(coder: NSCoder) {
  34. fatalError("init(coder:) has not been implemented")
  35. }
  36. override func layoutSubviews() {
  37. super.layoutSubviews()
  38. tableView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT - KNavBarHeight )
  39. if self.editingIndexPath != nil {
  40. configSwipeButtons()
  41. }
  42. }
  43. }
  44. extension IHGatewayListView:UITableViewDelegate,UITableViewDataSource{
  45. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  46. return self.devDataList?.count ?? 0
  47. }
  48. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  49. let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! IHEquipmentListCell
  50. cell.selectionStyle = .none
  51. cell.gateway = self.devDataList![indexPath.row]
  52. return cell
  53. }
  54. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  55. return 135
  56. }
  57. //iOS 11 将进入此方法 actions.performsFirstActionWithFullSwipe = NO 可以控制是否自动删除。
  58. func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
  59. let deleteAction = UIContextualAction(style: .normal, title: "") { (action, sourceView, completionHandler) in
  60. completionHandler(true)
  61. log.debug("删除")
  62. // if let delegate = self.delegate{
  63. // let devData = self.devDataList![indexPath.row]
  64. // delegate.equipmentListDeleteDevice(devData.devType!, devId: devData.id!, segmentIndex: self.currentIndex ?? 0)
  65. // }
  66. //处理事件
  67. }
  68. deleteAction.backgroundColor = UIColor(hexString: "#EBEFF2")
  69. let historyAction = UIContextualAction(style: .normal, title: "") { (action, sourceView, completionHandler) in
  70. completionHandler(true)
  71. //处理事件0
  72. log.debug("历史数据")
  73. // if let delegate = self.delegate{
  74. // // delegate.equipmentHistory(indexPath.row)
  75. // let data = self.devDataList![indexPath.row]
  76. // delegate.equipmentHistory(data, segmentIndex: self.currentIndex ?? 0)
  77. // }
  78. }
  79. // #05CFAB
  80. historyAction.backgroundColor = UIColor(hexString: "#573F95")
  81. let settingAction = UIContextualAction(style: .normal, title: "") { (action, sourceView, completionHandler) in
  82. completionHandler(true)
  83. //处理事件
  84. log.debug("设置")
  85. // if let delegate = self.delegate{
  86. // let data = self.devDataList![indexPath.row]
  87. // delegate.equipmentSetting(data, segmentIndex: self.currentIndex ?? 0)
  88. // }
  89. }
  90. // #05CFAB
  91. settingAction.backgroundColor = UIColor(hexString: "#573F95")
  92. let actions = UISwipeActionsConfiguration(actions: [settingAction,historyAction,deleteAction])
  93. actions.performsFirstActionWithFullSwipe = false
  94. return actions
  95. }
  96. func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  97. }
  98. func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  99. return true
  100. }
  101. func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
  102. editingIndexPath = indexPath
  103. // log.debug("willBeginEditingRowAt")
  104. setNeedsLayout() // 触发layoutSubviews()
  105. }
  106. //取消选中
  107. func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
  108. editingIndexPath = nil
  109. // log.debug("didEndEditingRowAt")
  110. }
  111. func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
  112. // log.debug("canMoveRowAt")
  113. return true
  114. }
  115. func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
  116. // log.debug("sourceIndexPath - \(sourceIndexPath) --destinationIndexPath \(destinationIndexPath)")
  117. }
  118. func configSwipeButtons() {
  119. // log.debug("重新绘制 ")
  120. //iOS 11 层级 UITableView -> UISwipActionPull
  121. var swipeButtons : [UIView]?
  122. if #available(iOS 11, *) {
  123. swipeButtons = self.tableView.getSwipeButtonView()
  124. }else{
  125. // iOS 8-10: 画UITableView->UITableViewCell->UITableVIewCellDeleteConfirmationView
  126. let tabCell = self.tableView.cellForRow(at: self.editingIndexPath!)
  127. swipeButtons = tabCell?.getSwipeButtonView()
  128. }
  129. // log.debug("子控件 -- \(swipeButtons)")
  130. if swipeButtons?.count == 0 || swipeButtons == nil {
  131. return
  132. }
  133. for i in 0..<(swipeButtons?.count)!{
  134. let button = swipeButtons![i] as! UIButton
  135. if i == 0 {
  136. configSwipeButton(button, backgroundImage: UIImage(named: "ic_delete_bg")!)
  137. }else if i == 1{
  138. configSwipeButton(button, backgroundImage: UIImage(named: "ic_update")!)
  139. }else{
  140. configSwipeButton(button, backgroundImage: UIImage(named: "ic_setting_bg")!)
  141. }
  142. }
  143. }
  144. func configSwipeButton(_ button: UIButton,backgroundImage:UIImage) {
  145. button.layer.cornerRadius = 5
  146. button.layer.masksToBounds = true
  147. button.setBackgroundImage(backgroundImage, for: .normal)
  148. button.setBackgroundImage(backgroundImage, for: .selected)
  149. button.titleLabel?.font = UIFont(name: PingFangSC_Medium, size: 11)
  150. let frame = button.frame
  151. button.frame = CGRect(x: frame.origin.x + 5 , y: frame.origin.y + 10, width: frame.size.width - 10, height: frame.size.height - 20)
  152. }
  153. }