Skip to content

Instantly share code, notes, and snippets.

@lucaswkuipers
Last active March 29, 2024 01:33
Show Gist options
  • Save lucaswkuipers/4f7266ce79fbba4664e30c906f6c3ab4 to your computer and use it in GitHub Desktop.
Save lucaswkuipers/4f7266ce79fbba4664e30c906f6c3ab4 to your computer and use it in GitHub Desktop.
UIKit Dequeue cell extensions
import UIKit
extension UITableView {
func dequeue<Cell: UITableViewCell>(
_ reusableCellType: Cell.Type,
for indexPath: IndexPath,
identifier: String? = nil
) -> Cell {
let identifier = identifier ?? String(describing: reusableCellType)
register(reusableCellType, forCellReuseIdentifier: identifier)
guard let cell = dequeueReusableCell(withIdentifier: identifier, for: indexPath) as? Cell else {
fatalError("Unable to dequeue \(reusableCellType)")
}
return cell
}
}
extension UICollectionView {
func dequeue<Cell: UICollectionViewCell>(
_ reusableCellType: Cell.Type,
for indexPath: IndexPath,
identifier: String? = nil
) -> Cell {
let identifier = identifier ?? String(describing: reusableCellType)
register(reusableCellType, forCellWithReuseIdentifier: identifier)
guard let cell = dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as? Cell else {
fatalError("Unable to dequeue \(reusableCellType)")
}
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment