Last active
February 4, 2016 17:04
-
-
Save morizotter/70a982dbf39926016174 to your computer and use it in GitHub Desktop.
UITableViewのDataSourceをprotocolで定義しておく 2 ref: http://qiita.com/morizotter/items/6785b5aefaa6ebe67e7c
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 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 } | |
} |
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 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 | |
} | |
} |
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
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") | |
]) | |
} |
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
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
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 | |
} | |
} |
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
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