KMAdvertisementTableView.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. @objcMembers 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: [KMAdvertisementModelSection]?
  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<KMAdvertisementModelSection>) {
  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. print("点击")
  89. let view = collectionView.item(at: indexPaths.first!) as! KMAdvertisementCollectionViewItem
  90. var content = view.model
  91. if actionCompletion != nil {
  92. content?.index = indexPaths.first?.item
  93. actionCompletion!(.tap, content!)
  94. }
  95. }
  96. //当item取消选中
  97. public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  98. let view = collectionView.item(at: indexPaths.first!) as! KMAdvertisementCollectionViewItem
  99. }
  100. }
  101. extension KMAdvertisementTableView: NSCollectionViewDataSource {
  102. public func numberOfSections(in collectionView: NSCollectionView) -> Int {
  103. return self.data?.count ?? 0
  104. }
  105. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  106. let model: KMAdvertisementModelSection = (self.data?[section])!
  107. return model.content!.count
  108. }
  109. //返回对应的item自定义个体
  110. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  111. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMAdvertisementCollectionViewItem"), for: indexPath) as! KMAdvertisementCollectionViewItem
  112. let model = self.data?[indexPath.section]
  113. view.model = model?.content![indexPath.item]
  114. return view
  115. }
  116. public func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView {
  117. var nibName: String?
  118. if kind == NSCollectionView.elementKindSectionHeader {
  119. nibName = "KMAdvertisementCollectionHeadView"
  120. }
  121. let view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: nibName!), for: indexPath)
  122. if let view = view as? KMAdvertisementCollectionHeadView {
  123. let model = self.data?[indexPath.section]
  124. view.model = model
  125. }
  126. return view
  127. }
  128. }
  129. extension KMAdvertisementTableView: NSCollectionViewDelegateFlowLayout {
  130. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  131. return NSSize(width: self.contentView.bounds.width, height: 32)
  132. }
  133. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> NSSize {
  134. return NSSize(width: 0, height: 30)
  135. }
  136. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  137. return 4
  138. }
  139. }