Skip to content

Instantly share code, notes, and snippets.

@chanonly123
Created May 2, 2020 07:15
Show Gist options
  • Save chanonly123/9e9d9cd1212d0243c3f1b52099208386 to your computer and use it in GitHub Desktop.
Save chanonly123/9e9d9cd1212d0243c3f1b52099208386 to your computer and use it in GitHub Desktop.
Dequeue UITableView cell using class name
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