Created
November 5, 2017 15:34
-
-
Save GenoZhou/f6b921275b5381251e7ecdd1a844abc0 to your computer and use it in GitHub Desktop.
Swift 4 UITableView extensions.
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
public protocol ReusableView: class { } | |
extension ReusableView where Self: UIView { | |
public static var reuseIdentifier: String { | |
return String(describing: self) | |
} | |
} | |
extension UITableViewCell: ReusableView { } | |
extension UITableViewHeaderFooterView: ReusableView { } | |
extension UITableView { | |
public func register<T: UITableViewCell>(cellClass: T.Type) { | |
register(cellClass, forCellReuseIdentifier: T.reuseIdentifier) | |
} | |
public func register<T: UITableViewHeaderFooterView>(aClass: T.Type) { | |
register(aClass, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier) | |
} | |
public func dequeueResuableCell<T: UITableViewCell>(forIndexPath indexPath: IndexPath) -> T { | |
guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T else { | |
fatalError(" Could not dequeue cell with identifier: \(T.reuseIdentifier)") | |
} | |
return cell | |
} | |
public func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView>() -> T { | |
guard let view = dequeueReusableHeaderFooterView(withIdentifier: T.reuseIdentifier) as? T else { | |
fatalError("Could not dequeue header/footer view with identifier: \(T.reuseIdentifier)") | |
} | |
return view | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment