Skip to content

Instantly share code, notes, and snippets.

View CharlesNascimento's full-sized avatar

Charles Nascimento CharlesNascimento

View GitHub Profile
@CharlesNascimento
CharlesNascimento / UITableView+DequeueReusableCell
Created August 9, 2021 20:26
This extension makes use of Swift generics and protocols to simplify the call to an UITableView's dequeueReusableCell method.
// Before:
// let cell = self.tableView.dequeueReusableCell(withIdentifier: KeyValueCell.identifier) as? KeyValueCell
// After:
// let cell: KeyValueCell = dequeueReusableCell()
extension UITableView {
func dequeueReusableCell<T: UITableViewCell>(for indexPath: IndexPath) -> T? {
return dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T
}