Skip to content

Instantly share code, notes, and snippets.

@ateliercw
Created January 8, 2016 06:08
Show Gist options
  • Save ateliercw/7506eb4278a4536b4567 to your computer and use it in GitHub Desktop.
Save ateliercw/7506eb4278a4536b4567 to your computer and use it in GitHub Desktop.
final class OldTableDataSource: NSObject {
}
private extension OldTableDataSource {
enum Section: Int {
case Header, Body, Footer
static let numberOfSections = 3
enum HeaderRows: Int {
case Row1, Row2
static let numberOfRows = 2
}
enum BodyRows: Int {
case row1, row2, row3, row4
static let numberOfRows = 4
}
enum FooterRows: Int {
case row1, row2, row3, row4
static let numberOfRows = 4
}
}
// Because I'm not exhaustively covering the cases, I'm cheating with a generic
func cellForRow<RowType>(row: RowType) -> UITableViewCell {
return UITableViewCell()
}
}
extension OldTableDataSource: UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return Section.numberOfSections
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let section = Section(rawValue: section) else {
fatalError()
}
switch section {
case .Header: return Section.HeaderRows.numberOfRows
case .Body: return Section.BodyRows.numberOfRows
case .Footer: return Section.FooterRows.numberOfRows
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
guard let section = Section(rawValue: indexPath.section) else {
fatalError()
}
let cell: UITableViewCell
switch section {
case .Header:
guard let row = Section.HeaderRows.init(rawValue: indexPath.row) else {
fatalError()
}
cell = cellForRow(row)
case .Body:
guard let row = Section.BodyRows.init(rawValue: indexPath.row) else {
fatalError()
}
cell = cellForRow(row)
case .Footer:
guard let row = Section.FooterRows.init(rawValue: indexPath.row) else {
fatalError()
}
cell = cellForRow(row)
}
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment