Skip to content

Commit

Permalink
opening up init(cellFactory)
Browse files Browse the repository at this point in the history
  • Loading branch information
icanzilb committed Sep 7, 2019
1 parent 68c0680 commit 82500a2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,23 @@ public class CollectionViewItemsController<CollectionType>: NSObject, UICollecti
public var dataSource: UICollectionViewDataSource?

// MARK: - Init

/// An initializer that takes a cell type and identifier and configures the controller to dequeue cells
/// with that data and configures each cell by calling the developer provided `cellConfig()`.
/// - Parameter cellIdentifier: A cell identifier to use to dequeue cells from the source collection view
/// - Parameter cellType: A type to cast dequeued cells as
/// - Parameter cellConfig: A closure to call before displaying each cell
public init<CellType>(cellIdentifier: String, cellType: CellType.Type, cellConfig: @escaping CellConfig<Element, CellType>) where CellType: UICollectionViewCell {
cellFactory = { dataSource, collectionView, indexPath, value in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! CellType
cellConfig(cell, indexPath, value)
return cell
}
}

private init(cellFactory: @escaping CellFactory<Element>) {

/// An initializer that takes a closure expected to return a dequeued cell ready to be displayed in the collection view.
/// - Parameter cellFactory: A `(CollectionViewItemsController<CollectionType>, UICollectionView, IndexPath, Element) -> UICollectionViewCell` closure. Use the table input parameter to dequeue a cell and configure it with the `Element`'s data
public init(cellFactory: @escaping CellFactory<Element>) {
self.cellFactory = cellFactory
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public class TableViewItemsController<CollectionType>: NSObject, UITableViewData
public var dataSource: UITableViewDataSource?

// MARK: - Init

/// An initializer that takes a cell type and identifier and configures the controller to dequeue cells
/// with that data and configures each cell by calling the developer provided `cellConfig()`.
/// - Parameter cellIdentifier: A cell identifier to use to dequeue cells from the source table view
/// - Parameter cellType: A type to cast dequeued cells as
/// - Parameter cellConfig: A closure to call before displaying each cell
public init<CellType>(cellIdentifier: String, cellType: CellType.Type, cellConfig: @escaping CellConfig<Element, CellType>) where CellType: UITableViewCell {
cellFactory = { dataSource, tableView, indexPath, value in
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CellType
Expand All @@ -48,7 +54,10 @@ public class TableViewItemsController<CollectionType>: NSObject, UITableViewData
}
}

private init(cellFactory: @escaping CellFactory<Element>) {

/// An initializer that takes a closure expected to return a dequeued cell ready to be displayed in the table view.
/// - Parameter cellFactory: A `(TableViewItemsController<CollectionType>, UITableView, IndexPath, Element) -> UITableViewCell` closure. Use the table input parameter to dequeue a cell and configure it with the `Element`'s data
public init(cellFactory: @escaping CellFactory<Element>) {
self.cellFactory = cellFactory
}

Expand Down

0 comments on commit 82500a2

Please sign in to comment.