Skip to content

Instantly share code, notes, and snippets.

@danielgarbien
Created February 5, 2017 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielgarbien/e1bc95dd2bef1ab91a4572c1c9306c9d to your computer and use it in GitHub Desktop.
Save danielgarbien/e1bc95dd2bef1ab91a4572c1c9306c9d to your computer and use it in GitHub Desktop.
import UIKit
extension UITableView {
func dequeueReusableCell(withIdentifier identifier: String, registerNibIfNeededWithNibName nibName: String) -> UITableViewCell? {
guard let cell = dequeueReusableCell(withIdentifier: identifier) else {
register(UINib(nibName: nibName, bundle: nil), forCellReuseIdentifier: identifier)
return dequeueReusableCell(withIdentifier: identifier)
}
return cell
}
func dequeueReusableCellRegisterNibIfNeeded(nibName: String) -> UITableViewCell? {
return dequeueReusableCell(withIdentifier: nibName, registerNibIfNeededWithNibName: nibName)
}
func dequeueReusableCellRegisterNibIfNeeded<NibClass>(identifier: String = String(describing: NibClass.self), nibName: String = String(describing: NibClass.self)) -> NibClass? {
return dequeueReusableCell(withIdentifier: identifier,
registerNibIfNeededWithNibName: nibName) as? NibClass
}
func deququeReusableCellRegisterClassIfNeeded<CellClass>(identifier: String = String(describing: CellClass.self)) -> CellClass?
where CellClass: AnyObject {
guard let cell = dequeueReusableCell(withIdentifier: identifier) else {
register(CellClass.self, forCellReuseIdentifier: identifier)
return dequeueReusableCell(withIdentifier: identifier) as? CellClass
}
return cell as? CellClass
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment