Skip to content

Instantly share code, notes, and snippets.

@MattSHallatt
Last active January 11, 2019 13:32
Show Gist options
  • Save MattSHallatt/764cd90cb177011e8a6205df2fd749c2 to your computer and use it in GitHub Desktop.
Save MattSHallatt/764cd90cb177011e8a6205df2fd749c2 to your computer and use it in GitHub Desktop.
Easy addition of informative reuse identifiers
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