Skip to content

Instantly share code, notes, and snippets.

@RichAppz
Last active December 7, 2020 11:15
Show Gist options
  • Save RichAppz/120170283238b7105600cc9a4de82b65 to your computer and use it in GitHub Desktop.
Save RichAppz/120170283238b7105600cc9a4de82b65 to your computer and use it in GitHub Desktop.
UICollectionView+Extension
import Foundation
import UIKit
public extension UICollectionView {
/**
Register nibs faster by passing the type - if for some reason the `identifier` is different then it can be passed
- Parameter type: UICollectionView.Type
- Parameter identifier: String?
*/
func registerCell(type: UICollectionViewCell.Type, identifier: String? = nil) {
let cellId = String(describing: type)
register(UINib(nibName: cellId, bundle: nil), forCellWithReuseIdentifier: identifier ?? cellId)
}
/**
DequeueCell by passing the type of UICollectionViewCell and IndexPath
- Parameter type: UICollectionViewCell.Type
- Parameter indexPath: IndexPath
*/
func dequeueCell<T: UICollectionViewCell>(withType type: UICollectionViewCell.Type, for indexPath: IndexPath) -> T? {
return dequeueReusableCell(withReuseIdentifier: type.identifier, for: indexPath) as? T
}
}
public extension UICollectionReusableView {
static var identifier: String {
return String(describing: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment