- movies
- actors
- years
- mimics all functions of
UITableViewDataSource
- pure Swift to be able to use the power of protocols
- default implementations instead of optional functions
- Default identifier that we have registered with the UITableView
- Create the tableView and show it
TableViewDataSourceProxy
to pass every method from our own protocol to UIKit's version
struct DataSource: TableViewDataSource {}
extension DataSource {
//Fixed data
}
struct MovieDataSource: TableViewDataSource {
var strings = JamesBond.movieNames
}
public extension MovieDataSource {
.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
struct ActorDataSource: TableViewDataSource {
var strings = JamesBond.actorNames
}
public extension ActorDataSource {
}
public protocol StringContaining {
var data: [String] { get }
}
extension TableViewDataSource where Self: StringContaining {
}
struct YearsDataSource: TableViewDataSource {
}
public protocol DataContaining {
typealias ItemType
var data: [ItemType] { get }
}
public extension DataContaining {
var numberOfItems: Int
func item(atIndexPath indexPath: NSIndexPath) -> ItemType?
}
public extension TableViewDataSource where Self: DataContaining
public protocol CellProviding {
func configure(cell: UITableViewCell, forItem item: ItemType, inTableView: UITableView) -> UITableViewCell
}
.
public extension CellProviding {
}
extension TableViewDataSource where Self: StringContaining, Self: CellProviding
extension Array: DataContaining {
public typealias ItemType = Element
public var data: [ItemType] {
return self
}
}
extension Array: DataSourceType, CellProviding, TableViewDataSource {}
struct MovieDataSource: DataContaining, TableViewDataSource, CellProviding {
typealias ItemType = Movie
var data = JamesBond.movies
}
func configure(cell: UITableViewCell, forItem item: ItemType, inTableView: UITableView) -> UITableViewCell {
cell.textLabel?.text = item.title
cell.detailTextLabel?.text = item.year.description
return cell
}
#SectionType
public protocol SectionType: DataContaining {}
#SectionContaining
public protocol SectionContaining {
typealias Section: SectionType
var sections: [Section] { get }
}
#Extend SectionContaining
public extension SectionContaining {
var numberOfSections: Int
func item(atIndexPath indexPath: NSIndexPath) -> Section.ItemType?
}
extension TableViewDataSource where Self: SectionContaining, Self: CellProviding, Self.Section.ItemType == Self.ItemType {
.
public func numberOfSectionsInTableView(tableView: UITableView) -> Int
#Conform Actor to SectionType
extension Actor: SectionType {
public typealias ItemType = Movie
public var data: [Movie] {
return movies
}
}
#Update MovieDataSource
struct MovieDataSource: SectionContaining, TableViewDataSource, CellProviding {
typealias Section = Actor
typealias ItemType = Movie
var sections = JamesBond.actors
}
#Add headerTitle to SectionType
public protocol SectionType: DataContaining {
var headerTitle: String? { get }
}
#Implement titleForHeaderInSection
public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?