Last active
February 2, 2016 15:40
-
-
Save barkoded/ea8d6c83e56824de4f40 to your computer and use it in GitHub Desktop.
Easy dequeuing using reusable views
This file contains 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
import UIKit | |
import MapKit | |
// MARK: - MKAnnotationView Extensions | |
extension MKAnnotationView: ReusableView {} | |
extension MKMapView { | |
@warn_unused_result | |
func dequeueReusableAnnotationViewWithAnnotation<T: MKAnnotationView where T: ReusableView>(annotation: MKAnnotation) -> T { | |
if let annotationView = dequeueReusableAnnotationViewWithIdentifier(T.defaultReuseIdentifier) as? T { | |
annotationView.annotation = annotation | |
return annotationView | |
} else { | |
return T(annotation: annotation, reuseIdentifier: T.defaultReuseIdentifier) | |
} | |
} | |
} | |
// MARK: - UITableView Extensions | |
extension UITableViewCell: ReusableView {} | |
extension UITableViewHeaderFooterView: ReusableView {} | |
extension UITableView { | |
func registerCell<T: UITableViewCell where T: ReusableView>(_: T.Type) { | |
registerClass(T.self, forCellReuseIdentifier: T.defaultReuseIdentifier) | |
} | |
func registerHeaderFooterView<T: UITableViewHeaderFooterView where T: ReusableView>(_: T.Type) { | |
registerClass(T.self, forHeaderFooterViewReuseIdentifier: T.defaultReuseIdentifier) | |
} | |
@warn_unused_result | |
func dequeueReusableCell<T: UITableViewCell where T: ReusableView>(forIndexPath indexPath: NSIndexPath) -> T { | |
guard let cell = dequeueReusableCellWithIdentifier(T.defaultReuseIdentifier, forIndexPath: indexPath) as? T else { | |
fatalError("Could not dequeue cell with identifier: '\(T.defaultReuseIdentifier)'") | |
} | |
return cell | |
} | |
@warn_unused_result | |
func dequeueReusableHeaderFooter<T: UITableViewHeaderFooterView where T: ReusableView>() -> T { | |
guard let view = dequeueReusableHeaderFooterViewWithIdentifier(T.defaultReuseIdentifier) as? T else { | |
fatalError("Could not dequeue UITableViewHeaderFooterView with identifier: '\(T.defaultReuseIdentifier)'") | |
} | |
return view | |
} | |
} | |
// MARK: - UICollectionView Extensions | |
extension UICollectionViewCell: ReusableView {} | |
extension UICollectionView { | |
func registerCell<T: UICollectionViewCell where T: ReusableView>(_: T.Type) { | |
registerClass(T.self, forCellWithReuseIdentifier: T.defaultReuseIdentifier) | |
} | |
func registerSupplementaryFooterView<T: UICollectionViewCell where T: ReusableView>(_: T.Type) { | |
registerClass(T.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: T.defaultReuseIdentifier) | |
} | |
func registerSupplementaryHeaderView<T: UICollectionViewCell where T: ReusableView>(_: T.Type) { | |
registerClass(T.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: T.defaultReuseIdentifier) | |
} | |
@warn_unused_result | |
func dequeueReusableCell<T: UICollectionViewCell where T: ReusableView>(forIndexPath indexPath: NSIndexPath) -> T { | |
guard let cell = dequeueReusableCellWithReuseIdentifier(T.defaultReuseIdentifier, forIndexPath: indexPath) as? T else { | |
fatalError("Could not dequeue cell with identifier: '\(T.defaultReuseIdentifier)'") | |
} | |
return cell | |
} | |
@warn_unused_result | |
func dequeueReusableSupplementaryViewOfKind<T: UICollectionViewCell where T: ReusableView>(kind: String, forIndexPath indexPath: NSIndexPath) -> T { | |
guard let view = dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: T.defaultReuseIdentifier, forIndexPath: indexPath) as? T else { | |
fatalError("Could not dequeue supplementary view of kind '\(kind)' with identifier: '\(T.defaultReuseIdentifier)'") | |
} | |
return view | |
} | |
} |
This file contains 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
import UIKit | |
protocol ReusableView: class { | |
static var defaultReuseIdentifier: String { get } | |
} | |
extension ReusableView where Self: UIView { | |
/** | |
Default reuse identifier which is usefull for registering and dequeing a `ReusableView` | |
from a view which supports dequeing of subviews like `UITableView` and `UICollectionView`. | |
- Return a default reuse identifier based on the name of the class. | |
*/ | |
static var defaultReuseIdentifier: String { | |
return String(self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment