IHPermissionView.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // IHPermissionView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/19.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. protocol IHTreeTableViewDelegate:NSObjectProtocol {
  10. func treeCellClick(node:IHTreeTableViewNode,indexPath:IndexPath)
  11. }
  12. class IHPermissionView: UIView {
  13. fileprivate let hotelID = "HotelCell"
  14. fileprivate let buildID = "buildCell"
  15. fileprivate let floodID = "floodCell"
  16. weak var treeDelegate:IHTreeTableViewDelegate?
  17. //外部传入的数据元(根节点集合)
  18. var rootNodes: [IHTreeTableViewNode] = [IHTreeTableViewNode](){
  19. didSet{
  20. getExpandNodeArray()
  21. self.tableView.reloadData()
  22. }
  23. }
  24. ///内部使用的数据源
  25. fileprivate var tempNodeArray : [IHTreeTableViewNode] = [IHTreeTableViewNode]()
  26. ///点击“展开”,添加的字节点的索引数组
  27. fileprivate var insertIndexPaths:[IndexPath] = [IndexPath]()
  28. private var insertRow = 0
  29. ///点击“收缩”,删除的子节点的“索引数组”
  30. fileprivate var deleteIndexPaths : [IndexPath] = [IndexPath]()
  31. lazy var tableView : UITableView = {
  32. let tableView = UITableView(frame: CGRect.zero, style: .plain)
  33. tableView.separatorStyle = .none
  34. tableView.backgroundColor = .white
  35. tableView.delegate = self
  36. tableView.dataSource = self
  37. tableView.register(UINib(nibName: "IHPermissionHeaderCell", bundle: nil), forCellReuseIdentifier: hotelID)
  38. tableView.register(UINib(nibName: "IHSecondaryCell", bundle: nil), forCellReuseIdentifier: buildID )
  39. tableView.register(UINib(nibName: "IHThirdGradeCell", bundle: nil), forCellReuseIdentifier: floodID)
  40. return tableView
  41. }()
  42. override init(frame: CGRect) {
  43. super.init(frame: frame)
  44. addSubview(tableView)
  45. }
  46. required init?(coder: NSCoder) {
  47. fatalError("init(coder:) has not been implemented")
  48. }
  49. override func layoutSubviews() {
  50. super.layoutSubviews()
  51. self.tableView.frame = self.bounds
  52. }
  53. /// 添加本节点及展开的子节点(包括展开的孙子节点)至数组中
  54. /// - Parameters
  55. /// - mode : 本节点
  56. func addExpandNodeToArray(node:IHTreeTableViewNode) {
  57. tempNodeArray.append(node)
  58. if node.isExpand { // 当前节点展开,添加其子节点
  59. for child in node.subNodes {
  60. addExpandNodeToArray(node: child)
  61. }
  62. }
  63. }
  64. ///获取所有展开的节点数组
  65. private func getExpandNodeArray(){
  66. for rootNode in rootNodes {
  67. if rootNode.perentNode == nil { //再次判断是否是根节点
  68. addExpandNodeToArray(node: rootNode)
  69. }
  70. }
  71. }
  72. ///是否是叶子结点
  73. ///
  74. /// - Parameter node :节点
  75. /// - Retures : ture - 是 ; false - 否
  76. fileprivate func isLeafNode(node:IHTreeTableViewNode)->Bool{
  77. return node.subNodes.count == 0
  78. }
  79. /// 点击”展开“,添加子节点(如果子节点也处于展开状态,添加子节点)
  80. ///
  81. /// - Parameter node :节点
  82. fileprivate func insertChildNode(node:IHTreeTableViewNode){
  83. node.isExpand = true
  84. if node.subNodes.count == 0 {
  85. return
  86. }
  87. insertRow = tempNodeArray.index(of: node)! + 1
  88. for childNode in node.subNodes {
  89. let childRow = insertRow
  90. let childIndexPath = IndexPath(row: childRow, section: 0)
  91. insertIndexPaths.append(childIndexPath)
  92. tempNodeArray.insert(childNode, at: childRow)
  93. insertRow += 1
  94. if childNode.isExpand {
  95. insertChildNode(node: childNode)
  96. }
  97. }
  98. }
  99. ///点击“收缩”,删除所有处于展开转台饿子节点索引
  100. /// - Parameter node :节点
  101. fileprivate func getDeleteIndexPaths(node:IHTreeTableViewNode){
  102. if node.isExpand {//再次确认节点处于展开状态
  103. for childNode in node.subNodes {
  104. let childRow = tempNodeArray.index(of: childNode)!
  105. let childIndexPath = IndexPath(row: childRow, section: 0)
  106. deleteIndexPaths.append(childIndexPath)
  107. if childNode.isExpand {
  108. getDeleteIndexPaths(node: childNode)
  109. }
  110. }
  111. }
  112. }
  113. /// 点击“收缩”,删除所有处于展开状态的子节点
  114. ///
  115. /// -Parameter node 节点
  116. fileprivate func deleteChildNode(node:IHTreeTableViewNode){
  117. getDeleteIndexPaths(node: node)
  118. node.isExpand = false
  119. for _ in deleteIndexPaths {
  120. tempNodeArray.remove(at: deleteIndexPaths.first!.row)
  121. }
  122. }
  123. }
  124. extension IHPermissionView:UITableViewDelegate,UITableViewDataSource{
  125. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  126. return tempNodeArray.count
  127. }
  128. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  129. return 0.01
  130. }
  131. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  132. return 0.01
  133. }
  134. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  135. let node = tempNodeArray[indexPath.row]
  136. if node.depth == 0{
  137. return 65
  138. }else if node.depth == 1{
  139. return 50
  140. }else{
  141. return 40
  142. }
  143. }
  144. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  145. let node = tempNodeArray[indexPath.row]
  146. let isLeaf = isLeafNode(node: node)
  147. if node.depth == 0{
  148. let cell = tableView.dequeueReusableCell(withIdentifier: hotelID) as! IHPermissionHeaderCell
  149. cell.selectionStyle = .none
  150. cell.isLeaf = isLeaf
  151. cell.node = node
  152. return cell
  153. }else if node.depth == 1{
  154. let cell = tableView.dequeueReusableCell(withIdentifier: buildID) as! IHSecondaryCell
  155. cell.selectionStyle = .none
  156. cell.isLeaf = isLeaf
  157. cell.node = node
  158. return cell
  159. }else{
  160. let cell = tableView.dequeueReusableCell(withIdentifier: floodID) as! IHThirdGradeCell
  161. cell.selectionStyle = .none
  162. cell.isLeaf = isLeaf
  163. cell.node = node
  164. return cell
  165. }
  166. return UITableViewCell()
  167. }
  168. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  169. let node = tempNodeArray[indexPath.row]
  170. if node.depth == 0{
  171. let cell = tableView.cellForRow(at: indexPath) as! IHPermissionHeaderCell
  172. cell.isExpand = node.isExpand
  173. }else if node.depth == 1{
  174. let cell = tableView.cellForRow(at: indexPath) as! IHSecondaryCell
  175. cell.isExpand = node.isExpand
  176. }else{
  177. let cell = tableView.cellForRow(at: indexPath) as! IHThirdGradeCell
  178. cell.isExpand = node.isExpand
  179. }
  180. treeDelegate?.treeCellClick(node: node, indexPath: indexPath)
  181. if isLeafNode(node: node) { //叶子节点
  182. return
  183. }else{//节点cell
  184. if node.isExpand {// 当前节点展开,要进行收缩操作
  185. deleteIndexPaths = [IndexPath]()
  186. deleteChildNode(node: node)
  187. self.tableView.deleteRows(at: deleteIndexPaths, with: .top)
  188. }else{ //当前节点搜索,要进行展开操作
  189. insertIndexPaths = [IndexPath]()
  190. insertChildNode(node: node)
  191. self.tableView.insertRows(at: insertIndexPaths, with: .top)
  192. }
  193. }
  194. }
  195. }