Skip to content

Commit

Permalink
Merge pull request #58 from BottleRocketStudios/configurableCells
Browse files Browse the repository at this point in the history
Add Configurable support for reusable view extensions
  • Loading branch information
tylermilner committed May 10, 2019
2 parents 29e3f11 + 0e602b7 commit 20676fc
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

##### Enhancements

* Add support for `Configurable` types when dequeuing reusable views.
[Will McGinty](https://github.com/willmcginty)
[#53](https://github.com/BottleRocketStudios/iOS-UtiliKit/pull/58)

##### Bug Fixes

* None.
* None


## 1.4.0 (2019-04-30)
Expand Down
29 changes: 28 additions & 1 deletion Sources/UtiliKit/Instantiation/MKMapView+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,24 @@ public extension MKMapView {
return annotationView
}

/**
Returns a reusable annotation view of type T.
- Parameter annotation: The annotation of the annotation view.
- Parameter element: An object used to configure the newly dequeued cell.
- Returns: An annotation view of type T configured with `element`.
*/
@available(iOS 11.0, *)
func dequeueReusableAnnotationView<T: MKAnnotationView & Configurable>(for annotation: MKAnnotation, configuredWith element: T.ConfiguringType) -> T {
let annotationView: T = dequeueReusableAnnotationView(for: annotation)
annotationView.configure(with: element)

return annotationView
}

/**
Returns a reusable, optional annotation view of type T.
- Parameter type: The type of the cell being registered. This should match the reuse identifier on the nib.
- Returns: Either a reusable annotation view of type T or a new instance of it.
*/
func dequeueReusableAnnotationView<T: MKAnnotationView>() -> T {
Expand All @@ -55,4 +69,17 @@ public extension MKMapView {

return T()
}

/**
Returns a reusable, optional annotation view of type T.
- Parameter element: An object used to configure the newly dequeued cell.
- Returns: Either a reusable annotation view of type T or a new instance of it, configured with `element`.
*/
func dequeueReusableAnnotationView<T: MKAnnotationView & Configurable>(configuredWith element: T.ConfiguringType) -> T {
let annotationView: T = dequeueReusableAnnotationView()
annotationView.configure(with: element)

return annotationView
}
}
29 changes: 29 additions & 0 deletions Sources/UtiliKit/Instantiation/UICollectionView+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ public extension UICollectionView {
return reusableCell
}

/**
Returns a reusable collection view cell of type T.
- Parameter indexPath: The index path of the cell.
- Parameter element: An object used to configure the newly dequeued cell.
- Returns: A collection view cell of type T configured with `element`.
*/
func dequeueReusableCell<T: UICollectionViewCell & Configurable>(for indexPath: IndexPath, configuredWith element: T.ConfiguringType) -> T {
let reusableCell: T = dequeueReusableCell(for: indexPath)
reusableCell.configure(with: element)

return reusableCell
}

/**
Returns a reusable supplementary view of type T.
Expand All @@ -144,4 +158,19 @@ public extension UICollectionView {

return reusableView
}

/**
Returns a reusable supplementary view of type T.
- Parameter kind: The kind of supplementary view to make.
- Parameter indexPath: The index path of the cell.
- Parameter element: An object used to configure the newly dequeued cell.
- Returns: A collection reusable view of type T configured with `element`.
*/
func dequeueReusableSupplementaryView<T: UICollectionReusableView & Configurable>(of kind: SupplementaryElementKind, for indexPath: IndexPath, configuredWith element: T.ConfiguringType) -> T {
let reusableView: T = dequeueReusableSupplementaryView(of: kind, for: indexPath)
reusableView.configure(with: element)

return reusableView
}
}
27 changes: 27 additions & 0 deletions Sources/UtiliKit/Instantiation/UITableView+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ public extension UITableView {
return reusableCell
}

/**
Returns a reusable table view cell of type T.
- Parameter indexPath: The index path of the cell.
- Parameter element: An object used to configure the newly dequeued cell.
- Returns: A table view cell of type T configured with `element`.
*/
func dequeueReusableCell<T: UITableViewCell & Configurable>(for indexPath: IndexPath, configuredWith element: T.ConfiguringType) -> T {
let reusableCell: T = dequeueReusableCell(for: indexPath)
reusableCell.configure(with: element)

return reusableCell
}

/**
Returns a reusable headerFooter view of type T.
Expand All @@ -73,4 +87,17 @@ public extension UITableView {

return reusableView
}

/**
Returns a reusable headerFooter view of type T.
- Parameter element: An object used to configure the newly dequeued header or footer view.
- Returns: A headerFooter view of type T configured with `element`.
*/
func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView & Configurable>(configuredWith element: T.ConfiguringType) -> T {
let reusableView: T = dequeueReusableHeaderFooterView()
reusableView.configure(with: element)

return reusableView
}
}

0 comments on commit 20676fc

Please sign in to comment.