import UIKit
enum TableViewSectionTypes {
case SectionType1
case SectionType2
case SectionType3
}
let tableViewCell1Identifier = "tableViewCell1Identifier"
let tableViewCell2Identifier = "tableViewCell2Identifier"
let tableViewCell3Identifier = "tableViewCell3Identifier"
class TableViewDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
let sections = [TableViewSectionTypes.SectionType2,TableViewSectionTypes.SectionType1,TableViewSectionTypes.SectionType3]
func registerTableViewCellIn(tableView: UITableView) {
tableView.register(UINib(nibName: "TableViewCellType1", bundle: nil), forCellReuseIdentifier: tableViewCell1Identifier)
tableView.register(UINib(nibName: "TableViewCellType2", bundle: nil), forCellReuseIdentifier: tableViewCell2Identifier)
tableView.register(UINib(nibName: "TableViewCellType3", bundle: nil), forCellReuseIdentifier: tableViewCell3Identifier)
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionType = sections[section]
switch sectionType {
case .SectionType1:
return 4
case .SectionType2:
return 8
case .SectionType3:
return 2
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let sectionType = sections[indexPath.section]
var tableViewCell: UITableViewCell
switch sectionType {
case .SectionType1:
let type1Cell = tableView.dequeueReusableCell(withIdentifier: tableViewCell1Identifier) as! TableViewCellType1
type1Cell.type1Label.text = "Type 1, Row \(indexPath.row)"
tableViewCell = type1Cell
case .SectionType2:
let type2Cell = tableView.dequeueReusableCell(withIdentifier:
tableViewCell2Identifier) as! TableViewCellType2
type2Cell.type2Label.text = "Type 2, Row \(indexPath.row)"
tableViewCell = type2Cell
case .SectionType3:
let type3Cell = tableView.dequeueReusableCell(withIdentifier: tableViewCell3Identifier) as! TableViewCellType3
type3Cell.type3Label.text = "Type 3, Row \(indexPath.row)"
tableViewCell = type3Cell
}
return tableViewCell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let sectionType = sections[section]
switch sectionType {
case .SectionType1:
return "Section 1 Type"
case .SectionType2:
return "Section 2 Type"
case .SectionType3:
return "Section 3 Type"
}
}
}