You can do it by extending your enum with a dynamic var SectionTitle
.
enum TabelSection: Int, CaseIterable {
case action
case drama
case comedy
var sectionTitle: String {
switch self {
case action: return "Action"
...
}
}
}
By making you enum an Int, it is possible to init it with a rawValue. The rawValue to init it is the section it you get from the TableView Method.
The CaseIterable is also quit nice so you can get the number cases (=sections) from it for the numberOfSections
Method.
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return TabelSection(rawValue: section)?.sectionTitle
}
Edit:
To display a headerView
for each section of the tableView change the TableViewstyle to Grouped
In your code just provide the tableView with the correct amount of sections.
override func numberOfSections(in tableView: UITableView) -> Int {
return TabelSection.allCases.count
}
If you really need a custom HeaderView you may use the following Method to do so
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
...
}