KMAdvertisementTableView.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // KMAdvertisementTableView.swift
  3. // KMAdvertisement
  4. //
  5. // Created by lizhe on 2022/11/28.
  6. //
  7. #if os(OSX)
  8. import AppKit
  9. #elseif os(iOS)
  10. import UIKit
  11. #endif
  12. open class KMAdvertisementTableView: KMAdvertisementBaseView {
  13. @IBOutlet var contentView: NSView!
  14. @IBOutlet weak var collectionView: NSCollectionView!
  15. lazy var presenter: KMAdvertisementTableViewPresenter! = KMAdvertisementTableViewPresenter()
  16. /**
  17. @abstract 外部传入数据
  18. @param inputData 文件路劲
  19. */
  20. open var inputData: KMAdvertisementModel! {
  21. didSet {
  22. self.presenter.initPresenter(view: self, data: inputData)
  23. }
  24. }
  25. //内部使用数据
  26. fileprivate var data: [KMAdvertisementModel.Section]?
  27. open override func draw(_ dirtyRect: NSRect) {
  28. super.draw(dirtyRect)
  29. // Drawing code here.
  30. }
  31. convenience init (data: KMAdvertisementModel, superView: NSView) {
  32. self.init(frame: superView.bounds)
  33. self.presenter.initPresenter(view: self, data: data)
  34. superView.addSubview(self)
  35. self.autoresizingMask = [.height , .width]
  36. // self.translatesAutoresizingMaskIntoConstraints = false
  37. // NSLayoutConstraint.activate([
  38. // self.topAnchor.constraint(equalTo: topAnchor),
  39. // self.leftAnchor.constraint(equalTo: leftAnchor),
  40. // self.rightAnchor.constraint(equalTo: rightAnchor),
  41. // self.bottomAnchor.constraint(equalTo: bottomAnchor)])
  42. // self.updateConstraintsForSubtreeIfNeeded()
  43. }
  44. public override init(frame frameRect: NSRect) {
  45. super.init(frame: frameRect)
  46. initContentView()
  47. setup()
  48. }
  49. // MARK: 初始化
  50. public required init?(coder decoder: NSCoder) {
  51. super.init(coder: decoder)
  52. initContentView()
  53. setup()
  54. }
  55. private func initContentView() {
  56. //绑定xib
  57. let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
  58. bundle: Bundle(for: self.classForCoder.self))!
  59. resource.instantiate(withOwner: self, topLevelObjects: nil)
  60. addSubview(contentView)
  61. contentView.translatesAutoresizingMaskIntoConstraints = false
  62. NSLayoutConstraint.activate([
  63. contentView.topAnchor.constraint(equalTo: topAnchor),
  64. contentView.leftAnchor.constraint(equalTo: leftAnchor),
  65. contentView.rightAnchor.constraint(equalTo: rightAnchor),
  66. contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  67. contentView.updateConstraintsForSubtreeIfNeeded()
  68. }
  69. func setup() {
  70. //设置代理
  71. self.collectionView.delegate = self
  72. self.collectionView.dataSource = self
  73. //是否可选中
  74. self.collectionView.isSelectable = true
  75. //注册cell
  76. self.collectionView.register(KMAdvertisementCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMAdvertisementCollectionViewItem"))
  77. }
  78. }
  79. extension KMAdvertisementTableView: KMAdvertisementTableViewPresenterDelegate {
  80. func showData(presenter: KMAdvertisementTableViewPresenter, data: Array<KMAdvertisementModel.Section>) {
  81. self.data = data
  82. self.collectionView.reloadData()
  83. }
  84. }
  85. extension KMAdvertisementTableView: NSCollectionViewDelegate {
  86. //当item被选中
  87. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  88. let view = collectionView.item(at: indexPaths.first!) as! KMAdvertisementCollectionViewItem
  89. var content = view.model
  90. if actionCompletion != nil {
  91. content?.index = indexPaths.first?.item
  92. actionCompletion!(content!)
  93. }
  94. }
  95. //当item取消选中
  96. public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  97. let view = collectionView.item(at: indexPaths.first!) as! KMAdvertisementCollectionViewItem
  98. }
  99. }
  100. extension KMAdvertisementTableView: NSCollectionViewDataSource {
  101. public func numberOfSections(in collectionView: NSCollectionView) -> Int {
  102. return self.data?.count ?? 0
  103. }
  104. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  105. let model: KMAdvertisementModel.Section = (self.data?[section])!
  106. return model.content.count
  107. }
  108. //返回对应的item自定义个体
  109. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  110. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMAdvertisementCollectionViewItem"), for: indexPath) as! KMAdvertisementCollectionViewItem
  111. let model = self.data?[indexPath.section]
  112. view.model = model?.content[indexPath.item]
  113. return view
  114. }
  115. public func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView {
  116. var nibName: String?
  117. if kind == NSCollectionView.elementKindSectionHeader {
  118. nibName = "KMAdvertisementCollectionHeadView"
  119. }
  120. let view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: nibName!), for: indexPath)
  121. if let view = view as? KMAdvertisementCollectionHeadView {
  122. let model = self.data?[indexPath.section]
  123. view.model = model
  124. }
  125. return view
  126. }
  127. }
  128. extension KMAdvertisementTableView: NSCollectionViewDelegateFlowLayout {
  129. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  130. return NSSize(width: self.contentView.bounds.width, height: 40)
  131. }
  132. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> NSSize {
  133. return NSSize(width: 0, height: 30)
  134. }
  135. }