Skip to content

Instantly share code, notes, and snippets.

@kitasuke
Created December 26, 2016 13:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitasuke/2845afb395e4e02a8b1cc98e5b46d1f5 to your computer and use it in GitHub Desktop.
Save kitasuke/2845afb395e4e02a8b1cc98e5b46d1f5 to your computer and use it in GitHub Desktop.
import UIKit
extension UITableView {
func registerNib<T: UITableViewCell>(forCellType type: T.Type) {
let name = String(describing: type)
let nib = UINib(nibName: name, bundle: nil)
register(nib, forCellReuseIdentifier: name)
}
func registerNib<T: UIView>(forHeaderFooterType type: T.Type) {
let name = String(describing: type)
let nib = UINib(nibName: name, bundle: nil)
register(nib, forHeaderFooterViewReuseIdentifier: name)
}
func registerClass<T: UITableViewCell>(forCellType type: T.Type) {
register(T.self, forCellReuseIdentifier: String(describing: type))
}
func registerClass<T: UIView>(forHeaderFooterType type: T.Type) {
register(T.self, forHeaderFooterViewReuseIdentifier: String(describing: type))
}
func dequeueReusableCell<T: UITableViewCell>(withType type: T.Type, for indexPath: IndexPath) -> T {
return dequeueReusableCell(withIdentifier: String(describing: type), for: indexPath) as! T
}
func dequeueReusableCell<T: UITableViewCell>(withType type: T.Type) -> T {
return dequeueReusableCell(withIdentifier: String(describing: type)) as! T
}
func dequeueReusableHeaderFooterView<T: UIView>(withType type: T.Type) -> T {
return dequeueReusableHeaderFooterView(withIdentifier: String(describing: type)) as! T
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment