Last active
October 20, 2021 01:37
-
-
Save jamesujeon/b6eee16f0a2c9be8dd718067363081ca to your computer and use it in GitHub Desktop.
Use class name for reuse identifier of something
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol Reusable { | |
static var reuseIdentifier: String { get } | |
} | |
extension Reusable { | |
static var reuseIdentifier: String { | |
String(describing: self) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
// MARK: - Reusable Cell | |
typealias ReusableCollectionViewCell = UICollectionViewCell & Reusable | |
extension UICollectionView { | |
func register(_ cellClass: ReusableCollectionViewCell.Type) { | |
register(cellClass, forCellWithReuseIdentifier: cellClass.reuseIdentifier) | |
} | |
func register(_ cellClasses: [ReusableCollectionViewCell.Type]) { | |
cellClasses.forEach(register) | |
} | |
func registerNib(_ cellClass: ReusableCollectionViewCell.Type, in bundle: Bundle? = nil) { | |
let nib = UINib(nibName: cellClass.reuseIdentifier, bundle: bundle) | |
register(nib, forCellWithReuseIdentifier: cellClass.reuseIdentifier) | |
} | |
func registerNib(_ cellClasses: [ReusableCollectionViewCell.Type], in bundle: Bundle? = nil) { | |
cellClasses.forEach { | |
registerNib($0, in: bundle) | |
} | |
} | |
func dequeueReusableCell<T: ReusableCollectionViewCell>(for indexPath: IndexPath) -> T { | |
guard let cell = dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as? T else { | |
fatalError("Failed to dequeue a reusable cell with \"\(T.reuseIdentifier)\"") | |
} | |
return cell | |
} | |
} | |
// MARK: - Reusable Supplementary View | |
typealias ReusableSupplementaryView = UICollectionReusableView & Reusable | |
extension UICollectionView { | |
func register(_ viewClass: ReusableSupplementaryView.Type, forSupplementaryViewOfKind elementKind: String) { | |
register(viewClass, forSupplementaryViewOfKind: elementKind, withReuseIdentifier: viewClass.reuseIdentifier) | |
} | |
func register(_ viewClasses: [ReusableSupplementaryView.Type], forSupplementaryViewOfKind elementKind: String) { | |
viewClasses.forEach { | |
register($0, forSupplementaryViewOfKind: elementKind) | |
} | |
} | |
func registerNib(_ viewClass: ReusableSupplementaryView.Type, forSupplementaryViewOfKind elementKind: String, in bundle: Bundle? = nil) { | |
let nib = UINib(nibName: viewClass.reuseIdentifier, bundle: bundle) | |
register(nib, forSupplementaryViewOfKind: elementKind, withReuseIdentifier: viewClass.reuseIdentifier) | |
} | |
func registerNib(_ viewClasses: [ReusableSupplementaryView.Type], forSupplementaryViewOfKind elementKind: String, in bundle: Bundle? = nil) { | |
viewClasses.forEach { | |
registerNib($0, forSupplementaryViewOfKind: elementKind, in: bundle) | |
} | |
} | |
func dequeueReusableSupplementaryView<T: ReusableSupplementaryView>(ofKind elementKind: String, for indexPath: IndexPath) -> T { | |
guard let view = dequeueReusableSupplementaryView(ofKind: elementKind, withReuseIdentifier: T.reuseIdentifier, for: indexPath) as? T else { | |
fatalError("Failed to dequeue a reusable supplementary view with \"\(T.reuseIdentifier)\"") | |
} | |
return view | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
// MARK: - Reusable Cell | |
typealias ReusableTableViewCell = UITableViewCell & Reusable | |
extension UITableView { | |
func register(_ cellClass: ReusableTableViewCell.Type) { | |
register(cellClass, forCellReuseIdentifier: cellClass.reuseIdentifier) | |
} | |
func register(_ cellClasses: [ReusableTableViewCell.Type]) { | |
cellClasses.forEach(register) | |
} | |
func registerNib(_ cellClass: ReusableTableViewCell.Type, in bundle: Bundle? = nil) { | |
let nib = UINib(nibName: cellClass.reuseIdentifier, bundle: bundle) | |
register(nib, forCellReuseIdentifier: cellClass.reuseIdentifier) | |
} | |
func registerNib(_ cellClasses: [ReusableTableViewCell.Type], in bundle: Bundle? = nil) { | |
cellClasses.forEach { | |
registerNib($0, in: bundle) | |
} | |
} | |
func dequeueReusableCell<T: ReusableTableViewCell>() -> T { | |
guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier) as? T else { | |
fatalError("Failed to dequeue a reusable cell with \"\(T.reuseIdentifier)\"") | |
} | |
return cell | |
} | |
func dequeueReusableCell<T: ReusableTableViewCell>(for indexPath: IndexPath) -> T { | |
guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T else { | |
fatalError("Failed to dequeue a reusable cell with \"\(T.reuseIdentifier)\"") | |
} | |
return cell | |
} | |
} | |
// MARK: - Reusable Header Footer View | |
typealias ReusableHeaderFooterView = UITableViewHeaderFooterView & Reusable | |
extension UITableView { | |
func register(_ viewClass: ReusableHeaderFooterView.Type) { | |
register(viewClass, forHeaderFooterViewReuseIdentifier: viewClass.reuseIdentifier) | |
} | |
func register(_ viewClasses: [ReusableHeaderFooterView.Type]) { | |
viewClasses.forEach(register) | |
} | |
func registerNib(_ viewClass: ReusableHeaderFooterView.Type, in bundle: Bundle? = nil) { | |
let nib = UINib(nibName: viewClass.reuseIdentifier, bundle: bundle) | |
register(nib, forHeaderFooterViewReuseIdentifier: viewClass.reuseIdentifier) | |
} | |
func registerNib(_ viewClasses: [ReusableHeaderFooterView.Type], in bundle: Bundle? = nil) { | |
viewClasses.forEach { | |
registerNib($0, in: bundle) | |
} | |
} | |
func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView & Reusable>() -> T { | |
guard let view = dequeueReusableHeaderFooterView(withIdentifier: T.reuseIdentifier) as? T else { | |
fatalError("Failed to dequeue a reusable header footer view with \"\(T.reuseIdentifier)\"") | |
} | |
return view | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment