Skip to content

Instantly share code, notes, and snippets.

@thejohnlima
Created March 5, 2022 20:11
Show Gist options
  • Save thejohnlima/a42537e047e99f3259596c921fcb55ec to your computer and use it in GitHub Desktop.
Save thejohnlima/a42537e047e99f3259596c921fcb55ec to your computer and use it in GitHub Desktop.
Extensions to help on UICollectionView implementation
import UIKit
extension UICollectionView {
/// Register cell
/// - Parameter cell: Cell to be registered
public func register(_ cell: UICollectionViewCell.Type) {
let nib = UINib(nibName: cell.identifier, bundle: nil)
register(nib, forCellWithReuseIdentifier: cell.identifier)
}
/// Register header
/// - Parameter reusableView: Collection header view to be registered
public func registerHeader(_ reusableView: UICollectionReusableView.Type) {
let nib = UINib(nibName: reusableView.identifier, bundle: nil)
register(
nib,
forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
withReuseIdentifier: reusableView.identifier
)
}
/// Register footer
/// - Parameter reusableView: Collection footer view to be registered
public func registerFooter(_ reusableView: UICollectionReusableView.Type) {
let nib = UINib(nibName: reusableView.identifier, bundle: nil)
register(
nib,
forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter,
withReuseIdentifier: reusableView.identifier
)
}
/// Setup reusable cell
/// - Parameters:
/// - class: Cell to reuse
/// - indexPath: Current index path
/// - configure: Configure cell if needed
/// - Returns: Configurable cell
public func dequeueReusableCell<T: UICollectionViewCell>(of class: T.Type,
for indexPath: IndexPath,
configure: ((T) -> Void)? = nil) -> UICollectionViewCell {
let cell = dequeueReusableCell(withReuseIdentifier: T.identifier, for: indexPath)
if let typedCell = cell as? T {
configure?(typedCell)
}
return cell
}
/// Setup reusable header cell
/// - Parameters:
/// - class: Header view to reuse
/// - indexPath: Current index path
/// - configure: Configure header if needed
/// - Returns: Configurable header view
public func dequeueReusableHeader<T: UICollectionReusableView>(of class: T.Type,
for indexPath: IndexPath,
configure: ((T) -> Void)? = nil) -> UICollectionReusableView {
let header = dequeueReusableSupplementaryView(
ofKind: UICollectionView.elementKindSectionHeader,
withReuseIdentifier: T.identifier,
for: indexPath
)
if let headerCell = header as? T {
configure?(headerCell)
}
return header
}
/// Setup reusable footer cell
/// - Parameters:
/// - class: Footer view to reuse
/// - indexPath: Current index path
/// - configure: Configure footer if needed
/// - Returns: Configurable footer view
public func dequeueReusableFooter<T: UICollectionReusableView>(of class: T.Type,
for indexPath: IndexPath,
configure: ((T) -> Void)? = nil) -> UICollectionReusableView {
let footer = dequeueReusableSupplementaryView(
ofKind: UICollectionView.elementKindSectionFooter,
withReuseIdentifier: T.identifier,
for: indexPath
)
if let footerCell = footer as? T {
configure?(footerCell)
}
return footer
}
}
extension NSObject {
public var identifier: String {
String(describing: type(of: self))
}
public static var identifier: String {
String(describing: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment