Skip to content

Instantly share code, notes, and snippets.

@justinmakaila
Created March 19, 2015 19:07
Show Gist options
  • Select an option

  • Save justinmakaila/18971b5aa3ce9d955d82 to your computer and use it in GitHub Desktop.

Select an option

Save justinmakaila/18971b5aa3ce9d955d82 to your computer and use it in GitHub Desktop.
UICollectionView+NibView
import UIKit
extension UICollectionView {
// MARK: - Dequeue Nib Views
func dequeueNibView<T: UICollectionViewCell where T: ReuseIdentifierProtocol>(view: T.Type, indexPath: NSIndexPath) -> T {
return dequeueReusableCellWithReuseIdentifier(view.reuseIdentifier(), forIndexPath: indexPath) as T
}
func dequeueSupplementaryReusableNibView<T: UICollectionReusableView where T: ReuseIdentifierProtocol>(view: T.Type, kind: String, indexPath: NSIndexPath) -> T {
return dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: view.reuseIdentifier(), forIndexPath: indexPath) as T
}
func dequeueReusableNibViewKind<T: UICollectionReusableView where T: ReusableNibViewKind>(view: T.Type, indexPath: NSIndexPath) -> T {
return dequeueSupplementaryReusableNibView(view, kind: view.reusableKind(), indexPath: indexPath)
}
// MARK: - Register Nib Views
func registerNibView<T: UICollectionViewCell where T: CellNibView>(view: T.Type) {
registerNibView(view, reuseIdentifier: view.reuseIdentifier())
}
func registerNibView<T: UICollectionViewCell where T: NibView>(view: T.Type, reuseIdentifier: String) {
registerNib(view.nib(), forCellWithReuseIdentifier: reuseIdentifier)
}
func registerReusableNibView<T: UICollectionReusableView where T: ReusableNibView>(view: T.Type, kind: String) {
registerReusableNibView(view, kind: kind, reuseIdentifier: view.reuseIdentifier())
}
func registerReusableNibView<T: UICollectionReusableView where T: NibView>(view: T.Type, kind: String, reuseIdentifier: String) {
registerNib(view.nib(), forSupplementaryViewOfKind: kind, withReuseIdentifier: reuseIdentifier)
}
func registerReusableNibView<T: UICollectionReusableView where T: ReusableNibViewKind>(view: T.Type) {
registerReusableNibView(view, kind: view.reusableKind())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment