Last active
March 29, 2024 01:33
-
-
Save lucaswkuipers/4f7266ce79fbba4664e30c906f6c3ab4 to your computer and use it in GitHub Desktop.
UIKit Dequeue cell extensions
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 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