Created
June 28, 2019 02:52
-
-
Save zeero/55ea6b90ecfa51c7c5d0fafdb661db94 to your computer and use it in GitHub Desktop.
Enum Table
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 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