12345678910111213141516171819202122232425262728293031323334353637 |
- //
- // KMReusable.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2023/12/11.
- //
- import Foundation
- public protocol KMReusable: AnyObject {
- static var identifier: String { get }
-
- static var itemIdentifier: NSUserInterfaceItemIdentifier { get }
- }
- extension KMReusable {
- public static var identifier: String {
- return String(describing: Self.self)
- }
-
- public static var itemIdentifier: NSUserInterfaceItemIdentifier {
- return NSUserInterfaceItemIdentifier(rawValue: self.identifier)
- }
- }
- extension KM where Base: NSCollectionView {
- public func register<T: NSCollectionViewItem>(cell: T.Type) where T: KMReusable {
- base.register(cell, forItemWithIdentifier: T.itemIdentifier)
- }
-
- public func dequeueReusableCell<T: NSCollectionViewItem>(for indexPath: IndexPath) -> T where T: KMReusable {
- guard let cell = base.makeItem(withIdentifier: T.itemIdentifier, for: indexPath) as? T else {
- fatalError("Could not dequeue reusable cell with identifier: \(T.itemIdentifier)")
- }
- return cell
- }
- }
|