KMAdvertisementTableView.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. let content = view.model
  97. guard let callBack = didSelect else { return }
  98. content?.index = indexPaths.first!.item
  99. callBack(self, content!)
  100. }
  101. //当item取消选中
  102. public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  103. _ = collectionView.item(at: indexPaths.first!) as! KMAdvertisementCollectionViewItem
  104. }
  105. }
  106. extension KMAdvertisementTableView: NSCollectionViewDataSource {
  107. public func numberOfSections(in collectionView: NSCollectionView) -> Int {
  108. return self.data.count
  109. }
  110. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  111. let model: KMAdvertisementItem = (self.data[section])
  112. return model.content!.count
  113. }
  114. //返回对应的item自定义个体
  115. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  116. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMAdvertisementCollectionViewItem"), for: indexPath) as! KMAdvertisementCollectionViewItem
  117. let model = self.data[indexPath.section]
  118. view.model = model.content![indexPath.item]
  119. return view
  120. }
  121. public func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView {
  122. var nibName: String?
  123. var view = NSView()
  124. if kind == NSCollectionView.elementKindSectionHeader {
  125. nibName = "KMAdvertisementCollectionHeadView"
  126. view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: nibName!), for: indexPath)
  127. if let view = view as? KMAdvertisementCollectionHeadView {
  128. let model = self.data[indexPath.section]
  129. view.model = model
  130. }
  131. } else if kind == NSCollectionView.elementKindSectionFooter {
  132. nibName = "KMAdvertisementCollectionHeadView"
  133. view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: nibName!), for: indexPath)
  134. }
  135. return view
  136. }
  137. }
  138. extension KMAdvertisementTableView: NSCollectionViewDelegateFlowLayout {
  139. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  140. // return NSSize(width: self.contentView.bounds.width, height: 32)
  141. return NSSize(width: 238, height: 32)
  142. }
  143. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> NSSize {
  144. return NSSize(width: 0, height: 30)
  145. }
  146. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForFooterInSection section: Int) -> NSSize {
  147. return NSSize(width: 0, height: 20)
  148. }
  149. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  150. return 4
  151. }
  152. }