BaseTableViewCell.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // BaseTransactionCell.swift
  3. // MTP2_iOS
  4. //
  5. // Created by zhongyuan on 2018/4/4.
  6. // Copyright © 2018年 zhongyuan.All rights reserved.
  7. //
  8. import UIKit
  9. import MGSwipeTableCell
  10. class BaseTableViewCell<Model>: MGSwipeTableCell {
  11. /// 模型数据
  12. var model: Model?
  13. /// cell所在的行
  14. var rowNum: Int?
  15. /// 携带信息
  16. var takeInfo: Any?
  17. /// 是否拓展
  18. var isExpland: Bool = false {
  19. didSet {
  20. /// 背景色
  21. self.contentView.backgroundColor = isExpland ? .F0F5F9() : .white
  22. }
  23. }
  24. /// 回调
  25. var block: ((_ model: Model?, _ sender: Any?) -> Void)?
  26. /// MARK: -
  27. private var subviewsArray: [UIView: UIColor] {
  28. var array: [UIView: UIColor] = [:]
  29. for subview in hierarchySubViews {
  30. if NSStringFromClass(type(of: subview)).contains("UITableViewCellContentView") { continue }
  31. if let backgroundColor = subview.backgroundColor,
  32. backgroundColor != UIColor.clear,
  33. backgroundColor != UIColor.white {
  34. array[subview] = backgroundColor
  35. }
  36. }
  37. return array
  38. }
  39. /// 缓存
  40. private lazy var storageSubViews: [UIView: UIColor] = subviewsArray
  41. override func setHighlighted(_ highlighted: Bool, animated: Bool) {
  42. super.setHighlighted(highlighted, animated: animated)
  43. for (view, color) in storageSubViews {
  44. view.backgroundColor = color
  45. }
  46. }
  47. override func setSelected(_ selected: Bool, animated: Bool) {
  48. super.setSelected(selected, animated: animated)
  49. for (view, color) in storageSubViews {
  50. view.backgroundColor = color
  51. }
  52. }
  53. }
  54. // MARK: - 获取所有子view的方法
  55. extension UIView {
  56. var hierarchySubViews: [UIView] {
  57. var array = [UIView]()
  58. if let stackView = self as? UIStackView {
  59. for subview in stackView.arrangedSubviews {
  60. array.append(subview)
  61. array += subview.hierarchySubViews
  62. }
  63. } else {
  64. for subview in subviews {
  65. array.append(subview)
  66. array += subview.hierarchySubViews
  67. }
  68. }
  69. return array
  70. }
  71. }