Created
May 2, 2020 07:15
-
-
Save chanonly123/9e9d9cd1212d0243c3f1b52099208386 to your computer and use it in GitHub Desktop.
Dequeue UITableView cell using class name
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<T: UITableViewCell>(type: T.Type) -> T { | |
let identifier = String(describing: type) | |
if let reusableCell = self.dequeueReusableCell(withIdentifier: identifier) { | |
if let cell = reusableCell as? T { | |
return cell | |
} else { | |
assertionFailure("tableview cell cannot be casted to \(identifier)") | |
} | |
} else { | |
assertionFailure("tableview cell not found: \(identifier)") | |
} | |
return T() | |
} | |
func register(nibType: AnyClass) { | |
let name = String(describing: nibType) | |
register(UINib(nibName: name, bundle: nil), forCellReuseIdentifier: name) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment