Skip to content

Instantly share code, notes, and snippets.

@jessesquires
Created February 8, 2015 01:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessesquires/2242d3c43753440f49ba to your computer and use it in GitHub Desktop.
Save jessesquires/2242d3c43753440f49ba to your computer and use it in GitHub Desktop.
public protocol TableViewCellFactoryType {
typealias DataItem
typealias Cell: UITableViewCell
func cellForItem(item: DataItem, inTableView tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> Cell
func configureCell(cell: Cell, forItem item: DataItem, inTableView tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> Cell
}
public struct TableViewCellFactory<Cell: UITableViewCell, DataItem>: TableViewCellFactoryType {
typealias CellConfigurationClosure = (Cell, DataItem, UITableView, NSIndexPath) -> Cell
private let reuseIdentifier: String
private let cellConfigurator: CellConfigurationClosure
public init(reuseIdentifier: String, cellConfigurator: CellConfigurationClosure) {
self.reuseIdentifier = reuseIdentifier
self.cellConfigurator = cellConfigurator
}
public func cellForItem(item: DataItem, inTableView tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> Cell {
let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as Cell
return configureCell(cell, forItem: item, inTableView: tableView, atIndexPath: indexPath)
}
public func configureCell(cell: Cell, forItem item: DataItem, inTableView tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> Cell {
return self.cellConfigurator(cell, item, tableView, indexPath)
}
}
public class TableViewDataSource <SectionCollection: CollectionType, CellFactory: TableViewCellFactoryType, DataItem
where
SectionCollection.Index == Int,
SectionCollection.Generator.Element: CollectionType,
SectionCollection.Generator.Element.Generator.Element == DataItem,
SectionCollection.Generator.Element.Index == Int,
CellFactory.DataItem == DataItem>: NSObject, UITableViewDataSource {
public var sections: SectionCollection
public let cellFactory: CellFactory
public init(sections: SectionCollection, cellFactory: CellFactory) {
self.sections = sections
self.cellFactory = cellFactory
}
// MARK: table view data source
public func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return countElements(sections)
}
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return countElements(sections[section])
}
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return cellFactory.cellForItem(sections[indexPath.section][indexPath.row], inTableView: tableView, atIndexPath: indexPath)
}
}
@jessesquires
Copy link
Author

Unfortunately this doesn't compile. 😢

If TableViewDataSource does not inherit from NSObject and does not conform to UITableViewDataSource, then this compiles successfully.

Thus, it looks like the only solution is to have a "proxy/bridgeDataSource" object as seen here:
https://gist.github.com/andymatuschak/f1e1691fa1a327468f8e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment