2016-02-12 1 views
0

Я хочу иметь TableView с несколькими клетками Prototype. Я нашел некоторые решения в Интернете, но у них слишком много повторяющихся кодов, особенно, поскольку моя система масштабируется. Как написать решение, которое работает как Similar StackOverflow question, но которое определяет ячейку вне коммутатора или условных операторов?Несколько прототипных ячеек в UITableView

Это рабочий код из раствора:

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 
} 

Я хочу, чтобы решение, которое выглядит как

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 
} 

ответ

2

Как вы не можете иметь полностью над кодом под switch случае, потому что есть не все вещи, общие для конкретной ячейки. Но, да, вы можете просто код таким образом:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

var cell: AnyObject? 

switch(indexPath.section){ 

    case 0: 

     cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! FlightsDetailCell 
     cell.userInteractionEnabled = false 
     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  

    case 1: 

     cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! FlightsDetailCell 
     cell.userInteractionEnabled = false 
     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 

    default: 
     cell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! FlightsInformationCell 
     ce.userInteractionEnabled = false 
    } 

    return cell 
} 

Надеюсь, это поможет!