KMAdvertisementTableView.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. typealias KMAdvertisementTableViewDidSelect = (_ view: KMAdvertisementTableView, _ item: KMAdvertisementItemInfo) -> Void
  13. @objcMembers open class KMAdvertisementTableView: NSView {
  14. @IBOutlet var contentView: NSView!
  15. @IBOutlet weak var collectionView: NSCollectionView!
  16. var didSelect: KMAdvertisementTableViewDidSelect?
  17. /**
  18. @abstract 外部传入数据
  19. @param inputData 文件路劲
  20. */
  21. open var inputData: KMAdvertisementContent? {
  22. didSet {
  23. self.reloadData()
  24. }
  25. }
  26. //内部使用数据
  27. fileprivate var data: [KMAdvertisementItem] = []
  28. open override func draw(_ dirtyRect: NSRect) {
  29. super.draw(dirtyRect)
  30. // Drawing code here.
  31. }
  32. convenience init (data: [KMAdvertisementItem], superView: NSView) {
  33. self.init(frame: superView.bounds)
  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. // self.backgroundColor(KMAppearance.Layout.l0Color())
  78. }
  79. func reloadData() {
  80. guard let inputData = inputData else { return }
  81. self.data.removeAll()
  82. if inputData.recommondContentPDFPro != nil && inputData.recommondContentPDFPro?.content?.count != 0 {
  83. self.data.append(inputData.recommondContentPDFPro!)
  84. }
  85. if inputData.recommondContentOther != nil && inputData.recommondContentOther?.content?.count != 0 {
  86. self.data.append(inputData.recommondContentOther!)
  87. }
  88. self.collectionView.reloadData()
  89. }
  90. }
  91. extension KMAdvertisementTableView: NSCollectionViewDelegate {
  92. //当item被选中
  93. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  94. print("点击")
  95. let view = collectionView.item(at: indexPaths.first!) as! KMAdvertisementCollectionViewItem
  96. self.collectionView.deselectItems(at: indexPaths)
  97. let content = view.model
  98. guard let callBack = didSelect else { return }
  99. content?.index = indexPaths.first!.item
  100. callBack(self, content!)
  101. }
  102. //当item取消选中
  103. public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  104. _ = collectionView.item(at: indexPaths.first!) as! KMAdvertisementCollectionViewItem
  105. }
  106. }
  107. extension KMAdvertisementTableView: NSCollectionViewDataSource {
  108. public func numberOfSections(in collectionView: NSCollectionView) -> Int {
  109. return self.data.count
  110. }
  111. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  112. let model: KMAdvertisementItem = (self.data[section])
  113. return model.content!.count
  114. }
  115. //返回对应的item自定义个体
  116. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  117. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMAdvertisementCollectionViewItem"), for: indexPath) as! KMAdvertisementCollectionViewItem
  118. let model = self.data[indexPath.section]
  119. view.model = model.content![indexPath.item]
  120. return view
  121. }
  122. public func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView {
  123. var nibName: String?
  124. var view = NSView()
  125. if kind == NSCollectionView.elementKindSectionHeader {
  126. nibName = "KMAdvertisementCollectionHeadView"
  127. view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: nibName!), for: indexPath)
  128. if let view = view as? KMAdvertisementCollectionHeadView {
  129. let model = self.data[indexPath.section]
  130. view.model = model
  131. }
  132. } else if kind == NSCollectionView.elementKindSectionFooter {
  133. nibName = "KMAdvertisementCollectionHeadView"
  134. view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: nibName!), for: indexPath)
  135. }
  136. return view
  137. }
  138. }
  139. extension KMAdvertisementTableView: NSCollectionViewDelegateFlowLayout {
  140. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  141. // return NSSize(width: self.contentView.bounds.width, height: 32)
  142. return NSSize(width: 238, height: 32)
  143. }
  144. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> NSSize {
  145. return NSSize(width: 0, height: 30)
  146. }
  147. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForFooterInSection section: Int) -> NSSize {
  148. return NSSize(width: 0, height: 20)
  149. }
  150. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  151. return 4
  152. }
  153. }