Skip to content

Instantly share code, notes, and snippets.

@morizotter
Last active February 4, 2016 17:04
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/70a982dbf39926016174 to your computer and use it in GitHub Desktop.
Save morizotter/70a982dbf39926016174 to your computer and use it in GitHub Desktop.
UITableViewのDataSourceをprotocolで定義しておく 2 ref: http://qiita.com/morizotter/items/6785b5aefaa6ebe67e7c
protocol TableViewSectionType {
typealias Row
var rows: [Row] { get set }
}
extension TableViewSectionType {
func title() -> String? {
return nil
}
func headerViewIdentifier() -> String? {
return nil
}
}
protocol TableViewDataSourceType {
typealias Section: TableViewSectionType
var sections: [Section] { get set }
}
enum SettingsCellType {
case Colors
case License
case InAppPurchase
case Restore
case Version
}
struct SettingItem {
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<T>: TableViewSectionType {
var rows: [T]
var title: String?
init(rows: [T], title: String?) {
self.rows = rows
self.title = title
}
}
struct SettingsDataSource<T: TableViewSectionType>: TableViewDataSourceType {
var sections: [T]
init(sections: [T]) {
self.sections = sections
}
}
var dataSource: SettingsDataSource<SettingsSection<SettingItem>>!
override func viewDidLoad() {
super.viewDidLoad()
dataSource = SettingsDataSource<SettingsSection<SettingItem>>(sections: [
SettingsSection<SettingItem>(rows: [
SettingItem(title: "Colors", subTitle: nil, cellType: .Colors)
], title: "General"),
SettingsSection<SettingItem>(rows: [
SettingItem(title: "InAppPurchase", subTitle: nil, cellType: .InAppPurchase),
SettingItem(title: "Restore", subTitle: nil, cellType: .Restore)
], title: "Purchase"),
SettingsSection<SettingItem>(rows: [
SettingItem(title: "License", subTitle: nil, cellType: .License),
SettingItem(title: "Version", subTitle: nil, cellType: .Version)
], title: "Application")
])
}
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
}
}
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
}
}
final class SettingsViewController: UITableViewController {
var dataSource: SettingsDataSource<SettingsSection<SettingItem>>!
override func viewDidLoad() {
super.viewDidLoad()
dataSource = SettingsDataSource<SettingsSection<SettingItem>>(sections: [
SettingsSection<SettingItem>(rows: [
SettingItem(title: "Colors", subTitle: nil, cellType: .Colors)
], title: "General"),
SettingsSection<SettingItem>(rows: [
SettingItem(title: "InAppPurchase", subTitle: nil, cellType: .InAppPurchase),
SettingItem(title: "Restore", subTitle: nil, cellType: .Restore)
], title: "Purchase"),
SettingsSection<SettingItem>(rows: [
SettingItem(title: "License", subTitle: nil, cellType: .License),
SettingItem(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, titleForHeaderInSection section: Int) -> String? {
return dataSource.sections[section].title
}
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
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment