Last active
January 11, 2019 13:32
-
-
Save MattSHallatt/764cd90cb177011e8a6205df2fd749c2 to your computer and use it in GitHub Desktop.
Easy addition of informative reuse identifiers
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 | |
protocol ReuseIdentifiable { | |
static var reuseIdentifier: String { get } | |
} | |
extension ReuseIdentifiable { | |
static var reuseIdentifier: String { | |
return String(describing: self) + "ReuseIdentifier" | |
} | |
} | |
extension UICollectionViewCell: ReuseIdentifiable { } | |
extension UITableViewCell: ReuseIdentifiable { } | |
extension UITableViewHeaderFooterView: ReuseIdentifiable { } | |
extension UICollectionView { | |
func register<C: UICollectionViewCell>(_ cellClass: C.Type) { | |
register(cellClass, forCellWithReuseIdentifier: cellClass.reuseIdentifier) | |
} | |
func dequeueReusableCell<C: UICollectionViewCell>(_ cellClass: C.Type, for indexPath: IndexPath) -> C { | |
return dequeueReusableCell(withReuseIdentifier: cellClass.reuseIdentifier, for: indexPath) as! C | |
} | |
} | |
extension UITableView { | |
func register<C: UITableViewCell>(_ cellClass: C.Type) { | |
register(cellClass, forCellReuseIdentifier: cellClass.reuseIdentifier) | |
} | |
func dequeueReusableCell<C: UITableViewCell>(_ cellClass: C.Type, for indexPath: IndexPath) -> C { | |
return dequeueReusableCell(withIdentifier: cellClass.reuseIdentifier, for: indexPath) as! C | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment