Last active
March 29, 2020 07:59
-
-
Save pranjalsatija/3dc8a7015881b004d7d3c03f6c5daec5 to your computer and use it in GitHub Desktop.
An example of how to make dequeuing and instantiating UITableViewCells a little nicer.
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
protocol IdentifiableCell { | |
static var identifier: String { get } | |
} | |
extension IdentifiableCell { | |
static var identifier: String { String(describing: Self.self) } | |
} | |
extension IdentifiableCell where Self: UITableViewCell { | |
static func dequeue(in tableView: UITableView) -> Self { | |
let nib = UINib(nibName: identifier, bundle: nil) | |
tableView.register(nib, forCellReuseIdentifier: identifier) | |
return tableView.dequeueReusableCell(withIdentifier: identifier) as! Self | |
} | |
} | |
class MyCell: UITableViewCell, IdentifiableCell { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment