Last active
July 20, 2021 10:58
-
-
Save smosko/d9e0cf7811f3475a9cf1c834a047b68d to your computer and use it in GitHub Desktop.
Reusable UITableView and UICollectionView cells
This file contains hidden or 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 | |
public protocol Reusable: AnyObject { | |
static var reuseIdentifier: String { get } | |
} | |
extension Reusable { | |
public static var reuseIdentifier: String { | |
String(describing: self) | |
} | |
} | |
extension UITableViewCell: Reusable {} | |
extension UITableViewHeaderFooterView: Reusable {} | |
extension UICollectionReusableView: Reusable {} | |
extension UITableView { | |
public func register<Cell: UITableViewCell>(_: Cell.Type) { | |
register(Cell.self, forCellReuseIdentifier: Cell.reuseIdentifier) | |
} | |
public func dequeue<Cell: UITableViewCell>(_: Cell.Type, for indexPath: IndexPath) -> Cell { | |
dequeueReusableCell(withIdentifier: Cell.reuseIdentifier, for: indexPath) as! Cell | |
} | |
public func register<View: UITableViewHeaderFooterView>(_: View.Type) { | |
register(View.self, forHeaderFooterViewReuseIdentifier: View.reuseIdentifier) | |
} | |
public func dequeue<View: UITableViewHeaderFooterView>(_: View.Type) -> View { | |
dequeueReusableHeaderFooterView(withIdentifier: View.reuseIdentifier) as! View | |
} | |
} | |
extension UICollectionView { | |
public func register<Cell: UICollectionViewCell>(_: Cell.Type) { | |
register(Cell.self, forCellWithReuseIdentifier: Cell.reuseIdentifier) | |
} | |
public func dequeue<Cell: UICollectionViewCell>(_: Cell.Type, for indexPath: IndexPath) -> Cell { | |
dequeueReusableCell(withReuseIdentifier: Cell.reuseIdentifier, for: indexPath) as! Cell | |
} | |
public func register<View: UICollectionReusableView>(_: View.Type) { | |
register( | |
View.self, | |
forSupplementaryViewOfKind: View.reuseIdentifier, | |
withReuseIdentifier: View.reuseIdentifier | |
) | |
} | |
public func dequeue<View: UICollectionReusableView>(_: View.Type, for indexPath: IndexPath) -> View { | |
dequeueReusableSupplementaryView( | |
ofKind: View.reuseIdentifier, | |
withReuseIdentifier: View.reuseIdentifier, | |
for: indexPath | |
) as! View | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment