Created
December 26, 2016 13:00
-
-
Save kitasuke/76bdc628273bce201aea63d7b0463a1a 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 | |
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