Skip to content

Instantly share code, notes, and snippets.

@morizotter
Last active February 4, 2016 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morizotter/d3b3f363a9700c8385f3 to your computer and use it in GitHub Desktop.
Save morizotter/d3b3f363a9700c8385f3 to your computer and use it in GitHub Desktop.
UITableViewのDataSourceをprotocolで定義しておく ref: http://qiita.com/morizotter/items/dc32d439b0060177cf79
protocol TableViewRowType {
}
protocol TableViewSectionType {
typealias Row: TableViewRowType
var rows: [Row] { get }
}
protocol TableViewDataSourceType {
typealias Section: TableViewSectionType
var sections: [Section] { get }
}
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
}
}
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")
])
}
...
// 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
}
}
}
// 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