Load Multiple Prototype Cells into UITableView

前端 未结 1 1535
鱼传尺愫
鱼传尺愫 2021-02-05 17:09

I currently have a UITableView containing 2 rows of a custom cell. I recently added a second prototype cell to my storyboard and have been attempting to add it to my UITableView

1条回答
  •  借酒劲吻你
    2021-02-05 18:08

    I set up a test app using code similar to what you have in cellForRowAtIndexPath, and I got the same results. Even though the code in my if indexPath.section == 2 clause was entered, the cell that was returned looked the same as my first two cells (but with no string in the label); this is despite the fact that I set it up with different sub views, and a log showed it to be the correct class for what I wanted for section 2. Why this happens, I don't know, but to fix it what you need to do is to dequeue the cell inside your if blocks like so.

    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
    }
    

    }

    This could be further refactored to take out repetitive code, but this should work...

    0 讨论(0)
提交回复
热议问题