IHGatewayView.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // IHGatewayView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/8/20.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. protocol IHGatewayViewDelete : NSObjectProtocol{
  10. func gatewaySelected(gateway : gatewayInfor)
  11. }
  12. class IHGatewayView: UIView {
  13. lazy var tableView: UITableView = {
  14. let tableView = UITableView(frame: self.frame, style: .plain)
  15. tableView.delegate = self
  16. tableView.dataSource = self
  17. tableView.allowsSelection = true
  18. tableView.separatorStyle = .none
  19. tableView.register(UINib(nibName: "IHGatewayCell", bundle: nil), forCellReuseIdentifier: "gatewayCell")
  20. return tableView
  21. }()
  22. var gatewayList : [gatewayInfor]?
  23. //编辑的cell
  24. var editingIndexPath : IndexPath?
  25. weak var delete : IHGatewayViewDelete?
  26. override init(frame: CGRect) {
  27. super.init(frame: frame)
  28. addSubview(tableView)
  29. }
  30. required init?(coder: NSCoder) {
  31. fatalError("init(coder:) has not been implemented")
  32. }
  33. override func layoutSubviews() {
  34. super.layoutSubviews()
  35. if self.editingIndexPath != nil {
  36. configSwipeButtons()
  37. }
  38. }
  39. }
  40. extension IHGatewayView : UITableViewDataSource,UITableViewDelegate{
  41. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  42. return gatewayList?.count ?? 0
  43. }
  44. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  45. let cell = tableView.dequeueReusableCell(withIdentifier: "gatewayCell") as! IHGatewayCell
  46. cell.backgroundColor = .white
  47. cell.selectionStyle = .none
  48. let gatawayInfor = gatewayList![indexPath.row]
  49. cell.gatawayInfor = gatawayInfor
  50. return cell
  51. }
  52. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  53. return 136
  54. }
  55. func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
  56. log.debug("左滑")
  57. #warning("快速移动的时候,如不加此方法,就有可能不执行layoutsubviews")
  58. self.tableView(self.tableView, willBeginEditingRowAt: indexPath)
  59. let deleteAction = UIContextualAction(style: .normal, title: "") { (action, sourceView, completionHandler) in
  60. completionHandler(true)
  61. log.debug("删除")
  62. // if self.currentIndex == 0{
  63. // if let devDataList = self.devDataList {
  64. // if indexPath.row < devDataList.count {
  65. // if let delegate = self.delegate{
  66. // let devData = self.devDataList![indexPath.row]
  67. // delegate.equipmentListDeleteDevice(devData.devType!, devId: devData.id!, segmentIndex: self.currentIndex ?? 0)
  68. // }
  69. // }else{
  70. // if let delegate = self.delegate {
  71. // // delegate.gatewayUpdate(segmentIndex: self.currentIndex ?? 5)
  72. // let gateway = self.gatewayList![indexPath.row - devDataList.count ]
  73. // delegate.gatewayDelegate(gateway, segmentIndex: self.currentIndex ?? 0)
  74. // }
  75. // }
  76. // }
  77. // }else if self.currentIndex == 5{
  78. // if let delegate = self.delegate {
  79. // let gateway = self.gatewayList![indexPath.row ]
  80. // delegate.gatewayDelegate(gateway, segmentIndex: self.currentIndex ?? 5)
  81. // }
  82. // }else{
  83. // if let delegate = self.delegate{
  84. // let devData = self.devDataList![indexPath.row]
  85. // delegate.equipmentListDeleteDevice(devData.devType!, devId: devData.id!, segmentIndex: self.currentIndex ?? 0)
  86. // }
  87. // }
  88. //
  89. //处理事件
  90. }
  91. deleteAction.backgroundColor = UIColor(hexString: "#EBEFF2")
  92. let historyAction = UIContextualAction(style: .normal, title: "") { (action, sourceView, completionHandler) in
  93. completionHandler(true)
  94. //处理事件0
  95. log.debug("历史数据")
  96. // if self.currentIndex == 0{
  97. // if let devDataList = self.devDataList {
  98. // if indexPath.row < devDataList.count {
  99. // if let delegate = self.delegate{
  100. // let data = self.devDataList![indexPath.row]
  101. // delegate.equipmentHistory(data, segmentIndex: self.currentIndex ?? 0)
  102. // }
  103. // }else{
  104. // if let delegate = self.delegate {
  105. // let data = self.gatewayList![indexPath.row - devDataList.count ]
  106. // delegate.gatewayUpdate(segmentIndex: self.currentIndex ?? 0)
  107. // }
  108. // }
  109. // }
  110. // }else if self.currentIndex == 5{
  111. // if let delegate = self.delegate {
  112. // let data = self.gatewayList![indexPath.row]
  113. // delegate.gatewayUpdate(segmentIndex: self.currentIndex ?? 5)
  114. // }
  115. // }else{
  116. // if let delegate = self.delegate{
  117. // let data = self.devDataList![indexPath.row]
  118. // delegate.equipmentHistory(data, segmentIndex: self.currentIndex ?? 0)
  119. // }
  120. // }
  121. //
  122. }
  123. //#31D4C1 #0A82CF
  124. historyAction.backgroundColor = UIColor(hexString: "#31D4C1")
  125. let settingAction = UIContextualAction(style: .normal, title: "") { (action, sourceView, completionHandler) in
  126. completionHandler(true)
  127. // if self.currentIndex == 0{
  128. // if let devDataList = self.devDataList {
  129. // if indexPath.row < devDataList.count {
  130. // if let delegate = self.delegate{
  131. // let data = self.devDataList![indexPath.row]
  132. // delegate.equipmentSetting(data, segmentIndex: self.currentIndex ?? 0)
  133. // }
  134. // }else{
  135. // if let delegate = self.delegate{
  136. // let data = self.gatewayList![indexPath.row - devDataList.count ]
  137. // delegate.gatewaySetting(data, segmentIndex: self.currentIndex ?? 5)
  138. // }
  139. // }
  140. // }
  141. // }else if self.currentIndex == 5{
  142. // if let delegate = self.delegate {
  143. // let data = self.gatewayList![indexPath.row ]
  144. // delegate.gatewaySetting(data, segmentIndex: self.currentIndex ?? 5)
  145. // }
  146. // }else{
  147. // log.debug("设置")
  148. // if let delegate = self.delegate{
  149. // let data = self.devDataList![indexPath.row]
  150. // delegate.equipmentSetting(data, segmentIndex: self.currentIndex ?? 0)
  151. // }
  152. //
  153. // }
  154. // //处理事件
  155. }
  156. //#05CFAB
  157. settingAction.backgroundColor = UIColor(hexString: "#573F95")
  158. let actions = UISwipeActionsConfiguration(actions: [settingAction,historyAction,deleteAction])
  159. actions.performsFirstActionWithFullSwipe = false
  160. return actions
  161. }
  162. func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  163. return false
  164. //return true
  165. }
  166. func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
  167. editingIndexPath = indexPath
  168. setNeedsLayout() // 触发layoutSubviews()
  169. }
  170. //取消选中
  171. func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
  172. editingIndexPath = nil
  173. // log.debug("didEndEditingRowAt")
  174. }
  175. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  176. let gatawayInfor = gatewayList![indexPath.row]
  177. self.delete?.gatewaySelected(gateway : gatawayInfor)
  178. }
  179. }
  180. extension IHGatewayView{
  181. func configSwipeButtons() {
  182. // log.debug("重新绘制 ")
  183. //iOS 11 层级 UITableView -> UISwipActionPull
  184. var swipeButtons : [UIView]?
  185. if #available(iOS 11, *) {
  186. swipeButtons = self.tableView.getSwipeButtonView()
  187. }else{
  188. // iOS 8-10: 画UITableView->UITableViewCell->UITableVIewCellDeleteConfirmationView
  189. let tabCell = self.tableView.cellForRow(at: self.editingIndexPath!)
  190. swipeButtons = tabCell?.getSwipeButtonView()
  191. }
  192. // log.debug("子控件 -- \(swipeButtons)")
  193. if swipeButtons?.count == 0 || swipeButtons == nil {
  194. return
  195. }
  196. for i in 0..<(swipeButtons?.count)!{
  197. let button = swipeButtons![i] as! UIButton
  198. if i == 0 {
  199. //ic_delete_bg
  200. configSwipeButton(button, backgroundImage: UIImage(named: "删除")!)
  201. }else if i == 1{
  202. configSwipeButton(button, backgroundImage: UIImage(named: "ic_update")!)
  203. }else{
  204. //ic_setting_bg
  205. configSwipeButton(button, backgroundImage: UIImage(named: "设置-紫")!)
  206. }
  207. }
  208. }
  209. func configSwipeButton(_ button: UIButton,backgroundImage:UIImage) {
  210. button.layer.cornerRadius = 5
  211. button.layer.masksToBounds = true
  212. button.setBackgroundImage(backgroundImage, for: .normal)
  213. button.setBackgroundImage(backgroundImage, for: .selected)
  214. button.titleLabel?.font = UIFont(name: PingFangSC_Medium, size: 11)
  215. let frame = button.frame
  216. button.frame = CGRect(x: frame.origin.x + 5 , y: frame.origin.y + 10, width: frame.size.width - 10, height: frame.size.height - 20)
  217. log.debug("frame\(button.frame)")
  218. }
  219. }