Skip to content

Instantly share code, notes, and snippets.

@kitasuke
Created December 26, 2016 13:00
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 kitasuke/76bdc628273bce201aea63d7b0463a1a to your computer and use it in GitHub Desktop.
Save kitasuke/76bdc628273bce201aea63d7b0463a1a to your computer and use it in GitHub Desktop.
import UIKit
extension UICollectionView {
func registerNib<T: UICollectionViewCell>(forCellType type: T.Type) {
let name = String(describing: type)
let nib = UINib(nibName: name, bundle: nil)
register(nib, forCellWithReuseIdentifier: name)
}
func registerNib<T: UIView>(forSupplementaryViewOfKind elementKind: String, withType type: T.Type) {
let name = String(describing: type)
let nib = UINib(nibName: name, bundle: nil)
register(nib, forSupplementaryViewOfKind: elementKind, withReuseIdentifier: name)
}
func registerClass<T: UICollectionViewCell>(forCellType type: T.Type) {
register(T.self, forCellWithReuseIdentifier: String(describing: type))
}
func registerClass<T: UIView>(forSupplementaryViewOfKind elementKind: String, withType type: T.Type) {
register(T.self, forSupplementaryViewOfKind: elementKind, withReuseIdentifier: String(describing: type))
}
func dequeueReusableCell<T: UICollectionViewCell>(withType type: T.Type, for indexPath: IndexPath) -> T {
return dequeueReusableCell(withReuseIdentifier: String(describing: type), for: indexPath) as! T
}
func dequeueReusableSupplementaryView<T: UIView>(ofKind elementKind: String, withType type: T.Type, for indexPath: IndexPath) -> T {
return dequeueReusableSupplementaryView(ofKind: elementKind, withReuseIdentifier: String(describing: type), for: indexPath) as! T
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment