BaseCollectionViewController.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. //
  2. // BaseCollectionViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Muchinfo on 2021/1/11.
  6. // Copyright © 2021 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import JXSegmentedView
  10. import IBAnimatable
  11. import SwiftyAttributes
  12. /// 列头宽度
  13. var columnWidth: CGFloat = 100.0
  14. /// 数据展示集合基类
  15. class BaseCollectionViewController: BaseViewController {
  16. // MARK: - 属性列表
  17. /// 操作视图
  18. @IBOutlet weak var operationView: UIStackView! {
  19. didSet {
  20. operationView.isHidden = true
  21. }
  22. }
  23. /// 类目
  24. @IBOutlet weak var segmentedView: JXSegmentedView! {
  25. didSet {
  26. segmentedView.indicators = [indicator]
  27. segmentedView.delegate = self
  28. }
  29. }
  30. /// 主滚动视图
  31. @IBOutlet weak var scrollView: UIScrollView!
  32. /// 阴影视图
  33. @IBOutlet weak var shadowView: AnimatableView!
  34. /// 坐标题
  35. @IBOutlet weak var leftTitle: UILabel!
  36. /// 列头头部视图
  37. @IBOutlet weak var headerCollectionView: UICollectionView! {
  38. didSet {
  39. /// 设置委托代理
  40. headerDelegate = HeaderDelegate(self)
  41. headerCollectionView.dataSource = headerDelegate
  42. headerCollectionView.delegate = headerDelegate
  43. /// 设置约束
  44. headerCollectionView.setCollectionViewLayout(flowLayout, animated: true)
  45. }
  46. }
  47. /// 左侧数据列表
  48. @IBOutlet weak var leftTableView: UITableView! {
  49. didSet {
  50. leftDelegate = LeftDelegate(self)
  51. leftTableView.dataSource = leftDelegate
  52. leftTableView.delegate = leftDelegate
  53. }
  54. }
  55. /// 右侧数据列表
  56. @IBOutlet weak var rightTableView: UITableView! {
  57. didSet {
  58. rightDelegate = RightDelegate(self)
  59. rightTableView.dataSource = rightDelegate
  60. rightTableView.delegate = rightDelegate
  61. }
  62. }
  63. /// 宽度约束
  64. @IBOutlet weak var widthLayoutConstraint: NSLayoutConstraint!
  65. /// 数据显示集合视图约束
  66. lazy var flowLayout: UICollectionViewFlowLayout = {
  67. /// 最小行间距,默认是0
  68. $0.minimumLineSpacing = 0
  69. /// 最小左右间距,默认是10
  70. $0.minimumInteritemSpacing = 0
  71. /// 区域内间距,默认是 UIEdgeInsetsMake(0, 0, 0, 0)
  72. $0.sectionInset = UIEdgeInsets(top: 0.0, left: 0, bottom: 0, right: 0)
  73. /// 水平滚动
  74. $0.scrollDirection = .horizontal
  75. return $0
  76. } (UICollectionViewFlowLayout())
  77. /// 列头数据
  78. var columnHeaders: [String] = [] {
  79. didSet {
  80. /// 刷新列表数据
  81. self.headerCollectionView.reloadData()
  82. /// 高度约束
  83. widthLayoutConstraint.constant = columnWidth*CGFloat(columnHeaders.count-1)
  84. /// 左侧标题
  85. leftTitle.text = columnHeaders.first
  86. }
  87. }
  88. /// 左侧数据信息
  89. var leftDatas: [String] = [] {
  90. didSet {
  91. /// 数据为空
  92. if leftDatas.count == 0, operationView == nil {
  93. operationView.isHidden = true
  94. }
  95. /// 刷新左侧列表数据
  96. leftTableView.reloadData()
  97. }
  98. }
  99. /// 右侧数据信息
  100. var rightDatas: [[String]] = []{
  101. didSet {
  102. /// 刷新左侧列表数据
  103. rightTableView.reloadData()
  104. }
  105. }
  106. /// banners
  107. var banners: [String] = []
  108. /// HeaderCellIdentifier
  109. let HeaderCellIdentifier = "Header_Cell"
  110. var headerDelegate: HeaderDelegate?
  111. /// LeftCellID
  112. let LeftCellID = "Left_Cell"
  113. var leftDelegate: LeftDelegate?
  114. /// RightCellID
  115. let RightCellID = "Right_Cell"
  116. var rightDelegate: RightDelegate?
  117. /// 执行回调
  118. var segmentBlock: ((_ index: Int, _ segment: JXSegmentedView) -> Void)?
  119. /// 操作回调
  120. var operatorBlock: ((_ type: OperatorType, _ takeInfo: Any?, _ row: Int?) -> Void)?
  121. /// 选中回调
  122. var selectBlock: ((_ row: Int?) -> Void)?
  123. /// 是否允许选中
  124. var canSelected: Bool = true
  125. /// 选中的位置
  126. var selectedRow: (row: Int, isExpland: Bool)? {
  127. didSet {
  128. /// 数据为空会异常
  129. if leftDatas.count == 0 || rightDatas.count == 0 {
  130. return
  131. }
  132. if let index = selectedRow {
  133. /// 刷新某一行
  134. leftTableView.reloadRows(at: [IndexPath(row: index.row, section: 0)], with: .automatic)
  135. /// 刷新某一行
  136. rightTableView.reloadRows(at: [IndexPath(row: index.row, section: 0)], with: .automatic)
  137. } else {
  138. /// 不为空
  139. if operationView != nil {
  140. /// 每次更新都隐藏
  141. operationView.isHidden = true
  142. }
  143. /// 重置选中行
  144. if let cell = leftTableView.visibleCells.first(where: { (obj) -> Bool in
  145. return (obj as? LeftCell)?.isExpland ?? false
  146. }) as? LeftCell {
  147. cell.isExpland = false
  148. }
  149. if let cell = rightTableView.visibleCells.first(where: { (obj) -> Bool in
  150. return (obj as? RightCell)?.isExpland ?? false
  151. }) as? RightCell {
  152. cell.isExpland = false
  153. }
  154. /// 重新刷新数据
  155. leftTableView.reloadData()
  156. rightTableView.reloadData()
  157. }
  158. }
  159. }
  160. /// 正常高度
  161. let normalTableHeight: CGFloat = 55.0
  162. /// 展开时高度
  163. let explaneTableHeight: CGFloat = 95.0
  164. // MARK: - 生命周期
  165. override func viewDidLoad() {
  166. super.viewDidLoad()
  167. // Do any additional setup after loading the view.
  168. /// 不需要返回按钮
  169. addBackBarButtonItem(true)
  170. }
  171. override func viewWillAppear(_ animated: Bool) {
  172. super.viewWillAppear(animated)
  173. /// 允许横屏
  174. let app = UIApplication.shared.delegate as? AppDelegate
  175. app?.isRotation = true
  176. /// 侦听横竖屏切换
  177. NotificationCenter.default.addObserver(self, selector: #selector(orientationDidChange(interfaceOrientation:)), name: UIDevice.orientationDidChangeNotification, object: nil)
  178. }
  179. override func viewDidAppear(_ animated: Bool) {
  180. super.viewDidAppear(animated)
  181. /// 清空数据
  182. selectedRow = nil
  183. }
  184. override func viewDidDisappear(_ animated: Bool) {
  185. super.viewDidDisappear(animated)
  186. /// 允许横屏
  187. let app = UIApplication.shared.delegate as? AppDelegate
  188. app?.isRotation = true
  189. /// 移出侦听
  190. NotificationCenter.default.removeObserver(self)
  191. }
  192. // MARK: - 交互相关
  193. /// onTapGestureRecognizer
  194. /// - Parameter sender: sender
  195. @IBAction fileprivate func onTapGestureRecognizer(_ sender: UITapGestureRecognizer) {
  196. let point = sender.location(in: rightTableView)
  197. /// 异常
  198. guard let index = rightTableView.indexPathForRow(at: point) else { return }
  199. /// 执行回调
  200. rightTableView.delegate?.tableView?(rightTableView, didSelectRowAt: index)
  201. }
  202. /// orientationDidChange
  203. /// - Parameter interfaceOrientation: interfaceOrientation
  204. @objc func orientationDidChange(interfaceOrientation: UIInterfaceOrientation) {
  205. if segmentedView != nil {
  206. /// 横竖屏切换时 重新计算
  207. segmentedView.width = self.view.width
  208. segmentedView.reloadData()
  209. }
  210. }
  211. }
  212. // MARK: - JXSegmentedViewDelegate
  213. extension BaseCollectionViewController: JXSegmentedViewDelegate {
  214. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  215. /// 清空数据
  216. selectedRow = nil
  217. /// 执行回调
  218. if let block = self.segmentBlock { block(index, segmentedView) }
  219. }
  220. }
  221. // MARK: - ColunmTitles、 UITableViewDelegate & UITableViewDataSource
  222. class HeaderDelegate: NSCoder, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  223. var owner: BaseCollectionViewController
  224. init(_ owner: BaseCollectionViewController) {
  225. self.owner = owner
  226. }
  227. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  228. return self.owner.columnHeaders.count-1
  229. }
  230. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  231. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.owner.HeaderCellIdentifier, for: indexPath) as! HeaderCell
  232. cell.headerTitle.text = owner.columnHeaders[indexPath.row+1]
  233. return cell
  234. }
  235. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  236. return CGSize(width: columnWidth, height: 35.0)
  237. }
  238. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  239. self.owner.scrollView.contentOffset.x = scrollView.contentOffset.x
  240. }
  241. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {}
  242. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {}
  243. }
  244. // MARK: - 左边 UITableViewDelegate & UITableViewDataSource
  245. class LeftDelegate: NSCoder, UITableViewDelegate, UITableViewDataSource {
  246. var owner: BaseCollectionViewController
  247. init(_ owner: BaseCollectionViewController) {
  248. self.owner = owner
  249. }
  250. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  251. return self.owner.leftDatas.count
  252. }
  253. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  254. let cell = tableView.dequeueReusableCell(withIdentifier: owner.LeftCellID, for: indexPath) as! LeftCell
  255. /// 所在的行数
  256. cell.rowNum = indexPath.row
  257. /// 执行回调
  258. cell.operatorBlock = { (_ type: OperatorType, _ takeInfo: Any?, _ row: Int?) in
  259. if let block = self.owner.operatorBlock { block(type, takeInfo, row) }
  260. }
  261. cell.model = owner.leftDatas[indexPath.row]
  262. return cell
  263. }
  264. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  265. /// 防止数据为空异常
  266. if owner.leftDatas.count == 0 || owner.rightDatas.count == 0 { return }
  267. /// 先清除选中状态
  268. owner.leftTableView.visibleCells.filter({($0 as? LeftCell)?.isExpland == true}).forEach { (obj) in
  269. (obj as? LeftCell)?.isExpland = false
  270. }
  271. owner.rightTableView.visibleCells.filter({($0 as? RightCell)?.isExpland == true}).forEach { (obj) in
  272. (obj as? RightCell)?.isExpland = false
  273. }
  274. if let selected = owner.selectedRow, indexPath.row == selected.row {
  275. owner.selectedRow = (row: indexPath.row, !(selected.isExpland))
  276. } else {
  277. owner.selectedRow = (row: indexPath.row, true)
  278. }
  279. /// 执行选中回调
  280. if let select = owner.selectBlock { select(indexPath.row) }
  281. }
  282. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  283. if let index = owner.selectedRow {
  284. return (index.row == indexPath.row && index.isExpland == true && owner.canSelected) ? owner.explaneTableHeight : owner.normalTableHeight
  285. }
  286. return owner.normalTableHeight
  287. }
  288. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  289. owner.rightTableView.contentOffset.y = scrollView.contentOffset.y
  290. /// 执行回调
  291. if let bk = owner.selectBlock, let index = owner.selectedRow, index.isExpland {
  292. bk(index.row)
  293. }
  294. }
  295. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {}
  296. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {}
  297. }
  298. // MARK: - 右边 UITableViewDelegate & UITableViewDataSource
  299. class RightDelegate: NSCoder, UITableViewDelegate, UITableViewDataSource {
  300. var owner: BaseCollectionViewController
  301. init(_ owner: BaseCollectionViewController) {
  302. self.owner = owner
  303. }
  304. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  305. return owner.rightDatas.count
  306. }
  307. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  308. let cell = tableView.dequeueReusableCell(withIdentifier: self.owner.RightCellID, for: indexPath) as! RightCell
  309. cell.rowNum = indexPath.row
  310. cell.model = owner.rightDatas[indexPath.row]
  311. cell.operatorBlock = { (_ type: OperatorType, _ takeInfo: Any?, _ row: Int?) in
  312. /// 执行回调
  313. if let block = self.owner.operatorBlock { block(type, takeInfo, row) }
  314. }
  315. return cell
  316. }
  317. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  318. /// 防止数据为空异常
  319. if owner.leftDatas.count == 0 || owner.rightDatas.count == 0 { return }
  320. /// 先清除选中状态
  321. owner.leftTableView.visibleCells.filter({($0 as? LeftCell)?.isExpland == true}).forEach { (obj) in
  322. (obj as? LeftCell)?.isExpland = false
  323. }
  324. owner.rightTableView.visibleCells.filter({($0 as? RightCell)?.isExpland == true}).forEach { (obj) in
  325. (obj as? RightCell)?.isExpland = false
  326. }
  327. if let selected = owner.selectedRow, indexPath.row == selected.row {
  328. owner.selectedRow = (row: indexPath.row, !(selected.isExpland))
  329. } else {
  330. owner.selectedRow = (row: indexPath.row, true)
  331. }
  332. /// 执行选中回调
  333. if let select = owner.selectBlock { select(indexPath.row) }
  334. }
  335. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  336. if let index = owner.selectedRow {
  337. return (index.row == indexPath.row && index.isExpland == true && owner.canSelected) ? owner.explaneTableHeight : owner.normalTableHeight
  338. }
  339. return owner.normalTableHeight
  340. }
  341. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  342. owner.leftTableView.contentOffset.y = scrollView.contentOffset.y
  343. /// 执行回调
  344. if let bk = owner.selectBlock, let index = owner.selectedRow, index.isExpland {
  345. bk(index.row)
  346. }
  347. }
  348. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {}
  349. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {}
  350. }
  351. // MARK: - UIScrollViewDelegate
  352. extension BaseCollectionViewController: UIScrollViewDelegate {
  353. /// scrollViewDidScroll
  354. /// - Parameter scrollView: scrollView
  355. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  356. /// contentOffset.x
  357. headerCollectionView.contentOffset.x = scrollView.contentOffset.x
  358. /// 左侧
  359. if scrollView.contentOffset.x<=0 { headerCollectionView.contentOffset.x = 0.0 }
  360. /// 执行回调
  361. if let bk = selectBlock, let index = selectedRow, index.isExpland {
  362. bk(index.row)
  363. }
  364. }
  365. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {}
  366. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {}
  367. }
  368. // MARK: - HeaderCell
  369. class HeaderCell: UICollectionViewCell {
  370. @IBOutlet weak var headerTitle: UILabel!
  371. }
  372. // MARK: - RightCell
  373. class RightCell: BaseTableViewCell<[String]> {
  374. /// 滚动视图
  375. @IBOutlet weak var stackView: UIStackView!
  376. /// 列头头部视图
  377. @IBOutlet weak var collectionView: UICollectionView! {
  378. didSet {
  379. /// 设置委托代理
  380. collectionView.dataSource = self
  381. collectionView.delegate = self
  382. /// 设置约束
  383. collectionView.setCollectionViewLayout(flowLayout, animated: true)
  384. /// 刷新数据
  385. collectionView.reloadData()
  386. }
  387. }
  388. /// 数据显示集合视图约束
  389. lazy var flowLayout: UICollectionViewFlowLayout = {
  390. /// 最小行间距,默认是0
  391. $0.minimumLineSpacing = 0
  392. /// 最小左右间距,默认是10
  393. $0.minimumInteritemSpacing = 0
  394. /// 区域内间距,默认是 UIEdgeInsetsMake(0, 0, 0, 0)
  395. $0.sectionInset = UIEdgeInsets(top: 0.0, left: 0, bottom: 0, right: 0)
  396. /// 水平滚动
  397. $0.scrollDirection = .horizontal
  398. return $0
  399. } (UICollectionViewFlowLayout())
  400. /// CellIdentifier
  401. fileprivate let CellIdentifier = "Collection_Cell"
  402. /// 操作回调
  403. var operatorBlock: ((_ type: OperatorType, _ takeInfo: Any?, _ row: Int?) -> Void)?
  404. /// 上层视图
  405. var father: BaseCollectionViewController?
  406. /// 数据模型
  407. override var model: [String]? {
  408. didSet {
  409. /// 异常
  410. guard let _ = model else { return }
  411. /// 刷新列表数据
  412. self.collectionView.reloadData()
  413. }
  414. }
  415. }
  416. // MARK: - UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
  417. extension RightCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  418. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  419. return self.model?.count ?? 0
  420. }
  421. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  422. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellIdentifier, for: indexPath) as! RightContentCell
  423. let titles = (self.model?[indexPath.item] ?? "\n").components(separatedBy: "\n")
  424. cell.titleLabel.text = titles.first?.isBlank()
  425. if titles.count > 1 {
  426. cell.detailLabel.text = titles.last?.isBlank()
  427. }
  428. cell.detailLabel.isHidden = titles.count <= 1
  429. return cell
  430. }
  431. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  432. return CGSize(width: columnWidth, height: 55.0)
  433. }
  434. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  435. /// 执行回调
  436. if let bk = operatorBlock { bk(.detail, nil, rowNum) }
  437. }
  438. }
  439. // MARK: - RightContentCell
  440. class RightContentCell: UICollectionViewCell {
  441. /// titleLabel
  442. @IBOutlet weak var titleLabel: UILabel!
  443. /// detailLabel
  444. @IBOutlet weak var detailLabel: UILabel!
  445. }
  446. // MARK: - LeftCell
  447. class LeftCell: BaseTableViewCell<Any> {
  448. /// 箭头
  449. @IBOutlet weak var arrow: AnimatableImageView!
  450. /// titleLabel
  451. @IBOutlet weak var titleLabel: UILabel!
  452. /// detailLabel
  453. @IBOutlet weak var detailLabel: UILabel!
  454. /// 是否拓展
  455. override var isExpland: Bool {
  456. didSet {
  457. arrow.image = UIImage(named: isExpland ? "arrow_up" : "arrow_down")
  458. }
  459. }
  460. /// 数据Model
  461. override var model: Any? {
  462. didSet {
  463. /// 异常
  464. guard let obj = model as? String else {
  465. return
  466. }
  467. /// 数据显示
  468. let titles = obj.components(separatedBy: "\n")
  469. titleLabel.text = titles.first?.isBlank()
  470. if titles.count > 1 { detailLabel.text = titles.last?.isBlank() }
  471. detailLabel.isHidden = titles.count <= 1
  472. }
  473. }
  474. /// 操作回调
  475. var operatorBlock: ((_ type: OperatorType, _ takeInfo: Any?, _ row: Int?) -> Void)?
  476. }