Created
April 21, 2017 15:01
-
-
Save iSapozhnik/c31ab629f61d42a4fa7e057e6f548416 to your computer and use it in GitHub Desktop.
Generic UITableView
This file contains hidden or 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
protocol Reusable: class { | |
static var reuseIdentifier: String { get } | |
static var nib: UINib? { get } | |
} | |
extension Reusable { | |
static var reuseIdentifier: String { return String(describing: self) } | |
static var nib: UINib? { return nil } | |
} | |
class CustomCell: UITableViewCell, Reusable { | |
} | |
extension UITableView { | |
func registerReusableCell<T: UITableViewCell>(_: T.Type) where T: Reusable { | |
if let nib = T.nib { | |
self.register(nib, forCellReuseIdentifier: T.reuseIdentifier) | |
} else { | |
self.register(T.self, forCellReuseIdentifier: T.reuseIdentifier) | |
} | |
} | |
func dequeueReusableCell<T: UITableViewCell>(_ indexPath: NSIndexPath) -> T where T: Reusable { | |
return self.dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath as IndexPath) as! T | |
} | |
func registerReusableHeaderFooterView<T: UITableViewHeaderFooterView>(_: T.Type) where T: Reusable { | |
if let nib = T.nib { | |
self.register(nib, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier) | |
} else { | |
self.register(T.self, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier) | |
} | |
} | |
func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView>() -> T? where T: Reusable { | |
return self.dequeueReusableHeaderFooterView(withIdentifier: T.reuseIdentifier) as! T? | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment