Skip to content

Instantly share code, notes, and snippets.

@zeero
Created June 28, 2019 02:52
Show Gist options
  • Save zeero/55ea6b90ecfa51c7c5d0fafdb661db94 to your computer and use it in GitHub Desktop.
Save zeero/55ea6b90ecfa51c7c5d0fafdb661db94 to your computer and use it in GitHub Desktop.
Enum Table
protocol EnumTable {
associatedtype TableSectionImpl: TableSection
}
extension EnumTable where Self: UITableViewController {
func tableRow(indexPath: IndexPath) -> TableRow? {
let section = TableSectionImpl(rawValue: indexPath.section)
return section?.tableRow(index: indexPath.row)
}
}
protocol TableSection: RawRepresentable where RawValue == Int {
func tableRow(index: Int) -> TableRow?
}
enum Section: Int, TableSection {
case first, second
func tableRow(index: Int) -> TableRow? {
switch self {
case .first:
return FirstSectionRow(rawValue: index)
case .second:
return SecondSectionRow(rawValue: index)
}
}
}
protocol TableRow {
init?(rawValue: Int)
var rawValue: Int { get }
}
enum FirstSectionRow: Int, TableRow {
case foo, bar
}
enum SecondSectionRow: Int, TableRow {
case baz
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment