Last active
February 19, 2018 03:13
-
-
Save akkyie/c65f260dd23102f6f5bca4e51a838bf8 to your computer and use it in GitHub Desktop.
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: 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