Created
February 8, 2015 01:29
-
-
Save jessesquires/2242d3c43753440f49ba to your computer and use it in GitHub Desktop.
Inspired by Andy Matuschak, https://gist.github.com/andymatuschak/f1e1691fa1a327468f8e
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
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately this doesn't compile. 😢
If
TableViewDataSource
does not inherit fromNSObject
and does not conform toUITableViewDataSource
, 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