Last active
February 4, 2016 16:53
-
-
Save morizotter/d3b3f363a9700c8385f3 to your computer and use it in GitHub Desktop.
UITableViewのDataSourceをprotocolで定義しておく ref: http://qiita.com/morizotter/items/dc32d439b0060177cf79
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
protocol TableViewRowType { | |
} | |
protocol TableViewSectionType { | |
typealias Row: TableViewRowType | |
var rows: [Row] { get } | |
} | |
protocol TableViewDataSourceType { | |
typealias Section: TableViewSectionType | |
var sections: [Section] { get } | |
} |
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
enum SettingsCellType { | |
case Colors | |
case License | |
case InAppPurchase | |
case Restore | |
case Version | |
} | |
struct SettingsRow: TableViewRowType { | |
let title: String? | |
let subTitle: String? | |
let cellType: SettingsCellType | |
init(title: String?, subTitle: String?, cellType: SettingsCellType) { | |
self.title = title | |
self.subTitle = subTitle | |
self.cellType = cellType | |
} | |
} | |
struct SettingsSection<Row: TableViewRowType>: TableViewSectionType { | |
let rows: [Row] | |
let title: String? | |
init(rows: [Row], title: String?) { | |
self.rows = rows | |
self.title = title | |
} | |
} | |
struct SettingsDataSource<Section: TableViewSectionType>: TableViewDataSourceType { | |
var sections: [Section] | |
init(sections: [Section]) { | |
self.sections = sections | |
} | |
} |
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 SettingsViewController: UITableViewController { | |
var dataSource: SettingsDataSource<SettingsSection<SettingsRow>>! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
dataSource = SettingsDataSource<SettingsSection<SettingsRow>>(sections: [ | |
SettingsSection<SettingsRow>(rows: [ | |
SettingsRow(title: "Colors", subTitle: nil, cellType: .Colors) | |
], title: "General"), | |
SettingsSection<SettingsRow>(rows: [ | |
SettingsRow(title: "InAppPurchase", subTitle: nil, cellType: .InAppPurchase), | |
SettingsRow(title: "Restore", subTitle: nil, cellType: .Restore) | |
], title: "Purchase"), | |
SettingsSection<SettingsRow>(rows: [ | |
SettingsRow(title: "License", subTitle: nil, cellType: .License), | |
SettingsRow(title: "Version", subTitle: nil, cellType: .Version) | |
], title: "Application") | |
]) | |
} | |
... |
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
// MARK: UITableViewDataSource | |
extension SettingsViewController { | |
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
return dataSource.sections.count | |
} | |
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return dataSource.sections[section].rows.count | |
} | |
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
let row = dataSource.sections[indexPath.section].rows[indexPath.row] | |
switch row.cellType { | |
case .Colors: | |
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) | |
cell.textLabel?.text = row.title | |
cell.detailTextLabel?.text = row.subTitle | |
cell.accessoryType = .DisclosureIndicator | |
return cell | |
case .InAppPurchase: | |
// Return cell | |
case .Restore: | |
// Return cell | |
case .License: | |
// Return cell | |
case .Version: | |
// Return cell | |
} | |
} | |
} |
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
// MARK: UITableViewDelegate | |
extension SettingsViewController { | |
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { | |
let row = dataSource.sections[indexPath.section].rows[indexPath.row] | |
switch row.cellType { | |
case .Colors: | |
// Do something | |
case .InAppPurchase: | |
// Do something | |
case .Restore: | |
// Do something | |
case .License: | |
// Do something | |
case .Version: | |
// Do something | |
} | |
} | |
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { | |
return dataSource.sections[section].title | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment