Last active
March 24, 2021 10:20
-
-
Save IanKeen/68eba888221a1a8de03dbbdd8a4dfcf1 to your computer and use it in GitHub Desktop.
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
class TableView { | |
weak var dataSource: TableViewDataSource? | |
} | |
class TableViewCell { } | |
protocol TableViewTitlesDataSource: class { | |
func tableView(tableView: TableView, titleForHeaderInSection section: Int) -> String? | |
func tableView(tableView: TableView, titleForFooterInSection section: Int) -> String? | |
} | |
protocol TableViewReorderingDataSource: class { | |
func tableView(tableView: TableView, canMoveRowAtIndexPath indexPath: IndexPath) -> Bool | |
func tableView(tableView: TableView, moveRowAtIndexPath sourceIndexPath: IndexPath, toIndexPath destinationIndexPath: IndexPath) | |
} | |
protocol TableViewDataSource: class { | |
func numberOfSections(in tableView: UITableView) -> Int | |
func tableView(tableView: TableView, numberOfRowsInSection section: Int) -> Int | |
func tableView(tableView: TableView, cellForRowAtIndexPath indexPath: IndexPath) -> TableViewCell | |
var titles: TableViewTitlesDataSource? { get } | |
var reordering: TableViewReorderingDataSource? { get } | |
} | |
// This provides auto opt-out by default | |
extension TableViewDataSource { | |
var titles: TableViewTitlesDataSource? { return nil } | |
var reordering: TableViewReorderingDataSource? { return nil } | |
} | |
// This provides auto opt-in when the `TableViewDataSource` is also a `TableViewTitlesDataSource` | |
extension TableViewDataSource where Self: TableViewTitlesDataSource { | |
var titles: TableViewTitlesDataSource? { return self } | |
} | |
// This provides auto opt-in when the `TableViewDataSource` is also a `TableViewReorderingDataSource` | |
extension TableViewDataSource where Self: TableViewReorderingDataSource { | |
var reordering: TableViewReorderingDataSource? { return self } | |
} | |
// Example of auto opt-in to `TableViewTitlesDataSource` | |
// (notice the `title` property isn't required as it's supplied by the conditional extension) | |
class MyDataSource: TableViewDataSource, TableViewTitlesDataSource { | |
func numberOfSections(in tableView: UITableView) -> Int { | |
return 0 | |
} | |
func tableView(tableView: TableView, numberOfRowsInSection section: Int) -> Int { | |
return 0 | |
} | |
func tableView(tableView: TableView, cellForRowAtIndexPath indexPath: IndexPath) -> TableViewCell { | |
return TableViewCell() | |
} | |
func tableView(tableView: TableView, titleForHeaderInSection section: Int) -> String? { | |
return nil | |
} | |
func tableView(tableView: TableView, titleForFooterInSection section: Int) -> String? { | |
return nil | |
} | |
} | |
let x = MyDataSource() | |
x.titles // not nil | |
x.reordering // nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment