BaseTableViewCell.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. class BaseTableViewCell<Model>: UITableViewCell {
  10. var model: Model? //模型数据
  11. /// 代理
  12. weak var delegate: BaseProtocal?
  13. /// cell所在的行
  14. var rowNum: Int?
  15. /// 携带信息
  16. var takeInfo: Any?
  17. /// MARK: -
  18. private var subviewsArray: [UIView: UIColor] {
  19. var array: [UIView: UIColor] = [:]
  20. for subview in hierarchySubViews {
  21. if NSStringFromClass(type(of: subview)).contains("UITableViewCellContentView") { continue }
  22. if let backgroundColor = subview.backgroundColor,
  23. backgroundColor != UIColor.clear,
  24. backgroundColor != UIColor.white {
  25. array[subview] = backgroundColor
  26. }
  27. }
  28. return array
  29. }
  30. /// 缓存
  31. private lazy var storageSubViews: [UIView: UIColor] = subviewsArray
  32. override func setHighlighted(_ highlighted: Bool, animated: Bool) {
  33. super.setHighlighted(highlighted, animated: animated)
  34. for (view, color) in storageSubViews {
  35. view.backgroundColor = color
  36. }
  37. }
  38. override func setSelected(_ selected: Bool, animated: Bool) {
  39. super.setSelected(selected, animated: animated)
  40. for (view, color) in storageSubViews {
  41. view.backgroundColor = color
  42. }
  43. }
  44. /// 选中相关
  45. /// - Parameter selected: selected
  46. func setCellSelected(_ selected: Bool) {
  47. self.contentView.backgroundColor = selected ? UIColorFromHex(rgbValue: 0x0A1E2F) : UIColorFromHex(rgbValue: 0x1e1e1e)
  48. }
  49. }
  50. // MARK: - 获取所有子view的方法
  51. extension UIView {
  52. var hierarchySubViews: [UIView] {
  53. var array = [UIView]()
  54. if let stackView = self as? UIStackView {
  55. for subview in stackView.arrangedSubviews {
  56. array.append(subview)
  57. array += subview.hierarchySubViews
  58. }
  59. } else {
  60. for subview in subviews {
  61. array.append(subview)
  62. array += subview.hierarchySubViews
  63. }
  64. }
  65. return array
  66. }
  67. }
  68. /// 基础协议,所有类代理必须继承该协议, 继承该协议是因为为了实现弱引用
  69. protocol BaseProtocal: class {
  70. }