Skip to content

Instantly share code, notes, and snippets.

@akkyie
Last active February 19, 2018 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akkyie/c65f260dd23102f6f5bca4e51a838bf8 to your computer and use it in GitHub Desktop.
Save akkyie/c65f260dd23102f6f5bca4e51a838bf8 to your computer and use it in GitHub Desktop.
import UIKit
public protocol Reusable: class {
static var reuseIdentifier: String { get }
}
extension UITableViewCell: Reusable {
public static var reuseIdentifier: String {
return NSStringFromClass(self)
}
}
extension UICollectionViewCell: Reusable {
public static var reuseIdentifier: String {
return NSStringFromClass(self)
}
}
protocol CellRegisterable {
associatedtype DefaultCell
func register(_ cellClass: AnyClass?, forCellReuseIdentifier identifier: String)
func dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> DefaultCell
}
extension CellRegisterable {
func register(_ cellClass: Reusable.Type) {
register(cellClass, forCellReuseIdentifier: cellClass.reuseIdentifier)
}
func dequeue<Cell: Reusable>(_ cellClass: Cell.Type, for indexPath: IndexPath) -> Cell? {
return dequeueReusableCell(withIdentifier: cellClass.reuseIdentifier, for: indexPath) as? Cell
}
}
extension UITableView: CellRegisterable {}
extension UICollectionView: CellRegisterable {
func register(_ cellClass: AnyClass?, forCellReuseIdentifier identifier: String) {
register(cellClass, forCellWithReuseIdentifier: identifier)
}
func dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UICollectionViewCell {
return dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment