Created
February 5, 2017 13:23
-
-
Save danielgarbien/e1bc95dd2bef1ab91a4572c1c9306c9d to your computer and use it in GitHub Desktop.
This file contains 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 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