123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- //
- // KMAdvertisementTableView.swift
- // KMAdvertisement
- //
- // Created by lizhe on 2022/11/28.
- //
- #if os(OSX)
- import AppKit
- #elseif os(iOS)
- import UIKit
- #endif
- typealias KMAdvertisementTableViewDidSelect = (_ view: KMAdvertisementTableView, _ item: KMAdvertisementItemInfo) -> Void
- @objcMembers open class KMAdvertisementTableView: NSView {
- @IBOutlet var contentView: NSView!
-
- @IBOutlet weak var collectionView: NSCollectionView!
-
- var didSelect: KMAdvertisementTableViewDidSelect?
- /**
- @abstract 外部传入数据
- @param inputData 文件路劲
- */
- open var inputData: KMAdvertisementContent? {
- didSet {
- self.reloadData()
- }
- }
- //内部使用数据
- fileprivate var data: [KMAdvertisementItem] = []
- open override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- // Drawing code here.
- }
-
- convenience init (data: [KMAdvertisementItem], superView: NSView) {
- self.init(frame: superView.bounds)
- superView.addSubview(self)
- self.autoresizingMask = [.height , .width]
- // self.translatesAutoresizingMaskIntoConstraints = false
- // NSLayoutConstraint.activate([
- // self.topAnchor.constraint(equalTo: topAnchor),
- // self.leftAnchor.constraint(equalTo: leftAnchor),
- // self.rightAnchor.constraint(equalTo: rightAnchor),
- // self.bottomAnchor.constraint(equalTo: bottomAnchor)])
- // self.updateConstraintsForSubtreeIfNeeded()
- }
-
- public override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- initContentView()
- setup()
- }
-
- // MARK: 初始化
- public required init?(coder decoder: NSCoder) {
- super.init(coder: decoder)
- initContentView()
- setup()
- }
- private func initContentView() {
- //绑定xib
- let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
- bundle: Bundle(for: self.classForCoder.self))!
- resource.instantiate(withOwner: self, topLevelObjects: nil)
- addSubview(contentView)
- contentView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- contentView.topAnchor.constraint(equalTo: topAnchor),
- contentView.leftAnchor.constraint(equalTo: leftAnchor),
- contentView.rightAnchor.constraint(equalTo: rightAnchor),
- contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
- contentView.updateConstraintsForSubtreeIfNeeded()
- }
-
- func setup() {
- //设置代理
- self.collectionView.delegate = self
- self.collectionView.dataSource = self
- //是否可选中
- self.collectionView.isSelectable = true
- //注册cell
- self.collectionView.register(KMAdvertisementCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMAdvertisementCollectionViewItem"))
-
- // self.backgroundColor(KMAppearance.Layout.l0Color())
- }
-
- func reloadData() {
- guard let inputData = inputData else { return }
-
- self.data.removeAll()
- if inputData.recommondContentPDFPro != nil && inputData.recommondContentPDFPro?.content?.count != 0 {
- self.data.append(inputData.recommondContentPDFPro!)
- }
-
- if inputData.recommondContentOther != nil && inputData.recommondContentOther?.content?.count != 0 {
- self.data.append(inputData.recommondContentOther!)
- }
-
- self.collectionView.reloadData()
- }
- }
- extension KMAdvertisementTableView: NSCollectionViewDelegate {
- //当item被选中
- public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
- print("点击")
- let view = collectionView.item(at: indexPaths.first!) as! KMAdvertisementCollectionViewItem
-
- self.collectionView.deselectItems(at: indexPaths)
-
- let content = view.model
-
- guard let callBack = didSelect else { return }
-
- content?.index = indexPaths.first!.item
- callBack(self, content!)
- }
-
- //当item取消选中
- public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
- _ = collectionView.item(at: indexPaths.first!) as! KMAdvertisementCollectionViewItem
- }
- }
- extension KMAdvertisementTableView: NSCollectionViewDataSource {
- public func numberOfSections(in collectionView: NSCollectionView) -> Int {
- return self.data.count
- }
-
- public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
- let model: KMAdvertisementItem = (self.data[section])
- return model.content!.count
- }
-
- //返回对应的item自定义个体
- public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
- let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMAdvertisementCollectionViewItem"), for: indexPath) as! KMAdvertisementCollectionViewItem
-
- let model = self.data[indexPath.section]
- view.model = model.content![indexPath.item]
- return view
- }
-
- public func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView {
- var nibName: String?
- var view = NSView()
- if kind == NSCollectionView.elementKindSectionHeader {
- nibName = "KMAdvertisementCollectionHeadView"
-
- view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: nibName!), for: indexPath)
- if let view = view as? KMAdvertisementCollectionHeadView {
- let model = self.data[indexPath.section]
- view.model = model
- }
- } else if kind == NSCollectionView.elementKindSectionFooter {
- nibName = "KMAdvertisementCollectionHeadView"
-
- view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: nibName!), for: indexPath)
- }
- return view
- }
- }
- extension KMAdvertisementTableView: NSCollectionViewDelegateFlowLayout {
-
- public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
- // return NSSize(width: self.contentView.bounds.width, height: 32)
- return NSSize(width: 238, height: 32)
- }
-
- public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> NSSize {
- return NSSize(width: 0, height: 30)
- }
-
- public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForFooterInSection section: Int) -> NSSize {
- return NSSize(width: 0, height: 20)
- }
-
- public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
- return 4
- }
- }
|