I want to have a TableView with multiple Prototype Cells. I've found some solutions online, but they have have too much repetitive code, especially as my system scales. How can I write a solution that works like Similar StackOverflow question but that defines a cell outside of the switch or conditional statements?
This is the working code from the solution:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
cell.userInteractionEnabled = false
if indexPath.section == 0 {
let cell: FlightsDetailCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as FlightsDetailCell
cell.graphView.enableBezierCurve = true
cell.graphView.enableReferenceYAxisLines = true
cell.graphView.enableYAxisLabel = true
cell.graphView.colorYaxisLabel = UIColor.whiteColor()
cell.graphView.delegate = UIApplication.sharedApplication().delegate as BEMSimpleLineGraphDelegate
cell.graphView.dataSource = UIApplication.sharedApplication().delegate as BEMSimpleLineGraphDataSource
return cell
}
else if indexPath.section == 1 {
let cell: FlightsDetailCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as FlightsDetailCell
cell.graphView.enableBezierCurve = true
cell.graphView.enableReferenceYAxisLines = true
cell.graphView.enableYAxisLabel = true
cell.graphView.colorYaxisLabel = UIColor.whiteColor()
cell.graphView.delegate = self
cell.graphView.dataSource = self
return cell
}
else {
let cell2: FlightsInformationCell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as FlightsInformationCell
cell2.userInteractionEnabled = false
return cell2
}
I want a solution that looks like
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UITableViewCell
switch (variable){
case "1":
cellIdentifier = "Identifier"
cell = cell as! MyTableViewCell
case "2":
cellIdentifier = "anotherIdentifier"
cell = cell as! MyOtherTableViewCell
}
cell.a.text = "hello"
cell.b.text = "hello"
...
return cell
}