Skip to content

Instantly share code, notes, and snippets.

@Dimillian
Created August 17, 2017 08:26
Show Gist options
  • Save Dimillian/baca6f42d120015eed83e7f479c2a19c to your computer and use it in GitHub Desktop.
Save Dimillian/baca6f42d120015eed83e7f479c2a19c to your computer and use it in GitHub Desktop.
public protocol Dequeueable {
static func id() -> String
static func hasNib() -> Bool
}
public extension UITableView {
func getCell<T: Dequeueable>(forType: T.Type) -> T? {
return dequeueReusableCell(withIdentifier: T.id()) as? T
}
func registerCell(withType cType: Dequeueable.Type) {
registerCells(withTypes: [cType])
}
func registerCells(withTypes cType: [Dequeueable.Type]) {
for ty in cType {
if ty.hasNib() {
register(UINib(nibName: ty.id(), bundle: Bundle.main), forCellReuseIdentifier: ty.id())
} else {
if let t = ty as? AnyClass {
register(t, forCellReuseIdentifier: ty.id())
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment