Last active
December 7, 2020 11:15
-
-
Save RichAppz/120170283238b7105600cc9a4de82b65 to your computer and use it in GitHub Desktop.
UICollectionView+Extension
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 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