Skip to content

Instantly share code, notes, and snippets.

@ateliercw
Last active January 8, 2016 14:15
Show Gist options
  • Save ateliercw/498f73d212d1ccb583de to your computer and use it in GitHub Desktop.
Save ateliercw/498f73d212d1ccb583de to your computer and use it in GitHub Desktop.
final class newTableViewDataSource: NSObject {
private var tableSections: [Section]
init(header: Bool, footer: Bool) {
var sections: [Section] = []
if header {
sections.append(Section.Header(rows: [
Section.Row.HeaderLead(title: "Header"),
Section.Row.StandardCell(title: "Test", detail: nil),
]))
}
sections.append(Section.Body(rows: [
Section.Row.StandardCell(title: "First Row", detail: nil),
Section.Row.StandardCell(title: "Row", detail: "With Detail"),
Section.Row.StandardCell(title: "More", detail: nil),
Section.Row.StandardCell(title: "Detail?", detail:"Yes"),
Section.Row.StandardCell(title: "Test", detail: nil),
]))
if footer {
sections.append(Section.Footer(rows: [
Section.Row.StandardCell(title: "Almost Done", detail: "Really!"),
Section.Row.FooterFinish(title: "...Done"),
]))
}
self.tableSections = sections
}
}
private extension newTableViewDataSource {
enum Section {
case Header(rows: [Row])
case Body(rows: [Row])
case Footer(rows: [Row])
enum Row {
case HeaderLead(title: String)
case StandardCell(title: String, detail: String?)
case FooterFinish(title: String)
}
}
}
extension newTableViewDataSource.Section {
var rows: [Row] {
switch self {
case .Body(let rows): return rows
case .Header(let rows): return rows
case .Footer(let rows): return rows
}
}
}
extension newTableViewDataSource.Section.Row {
var title: String {
switch self {
case .FooterFinish(let title): return title
case .HeaderLead(let title): return title
case .StandardCell(let title, _): return title
}
}
var detail: String? {
switch self {
case .FooterFinish, .HeaderLead: return nil
case .StandardCell(_, let detail): return detail
}
}
}
extension newTableViewDataSource: UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return tableSections.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableSections[section].rows.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let row = tableSections[indexPath.section].rows[indexPath.row]
let cell: UITableViewCell
switch row {
case .FooterFinish:
cell = tableView.dequeueReusableCellWithIdentifier("footerIdentifer", forIndexPath: indexPath)
case .HeaderLead:
cell = tableView.dequeueReusableCellWithIdentifier("headerIdentifier", forIndexPath: indexPath)
case .StandardCell:
cell = tableView.dequeueReusableCellWithIdentifier("standardIdentifier", forIndexPath: indexPath)
}
cell.textLabel?.text = row.title
cell.detailTextLabel?.text = row.detail
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment