Skip to content

Instantly share code, notes, and snippets.

@jeanetienne
Last active May 8, 2019 00:59
Show Gist options
  • Save jeanetienne/5e93609a05a2773ba6efd5ca8f430f33 to your computer and use it in GitHub Desktop.
Save jeanetienne/5e93609a05a2773ba6efd5ca8f430f33 to your computer and use it in GitHub Desktop.
//
// More type-aware convenience functions for registering and dequeueing cells
//
import UIKit
fileprivate extension UICollectionViewCell {
static var defaultReuseIdentifier: String {
return String(describing: self)
}
}
extension UICollectionView {
func register<Cell: UICollectionViewCell>(_ cellType: Cell.Type) {
register(cellType, forCellWithReuseIdentifier: cellType.defaultReuseIdentifier)
}
func dequeue<Cell: UICollectionViewCell>(_ cellType: Cell.Type, for indexPath: IndexPath) -> Cell? {
return dequeueReusableCell(withReuseIdentifier: cellType.defaultReuseIdentifier, for: indexPath) as? Cell
}
}
fileprivate extension UITableViewCell {
static var defaultReuseIdentifier: String {
return String(describing: self)
}
}
extension UITableView {
func register<Cell: UITableViewCell>(_ cellType: Cell.Type) {
register(cellType, forCellReuseIdentifier: cellType.defaultReuseIdentifier)
}
func dequeue<Cell: UITableViewCell>(_ cellType: Cell.Type, for indexPath: IndexPath) -> Cell? {
return dequeueReusableCell(withIdentifier: cellType.defaultReuseIdentifier, for: indexPath) as? Cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment