将多个原型单元加载到UITableView中

use*_*748 12 uitableview ios swift bemsimplelinegraph

我目前有一个包含2行自定义单元格的UITableView.我最近在我的故事板上添加了第二个原型单元,并且一直试图将它添加到我的UITableView中但没有成功.我的cellForRowAtIndexPAth方法如下:

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

    let cell: FlightsDetailCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as FlightsDetailCell

    cell.userInteractionEnabled = false

    if indexPath.section == 0 {

        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
    }

    if indexPath.section == 1 {
        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
    }

    if indexPath.section == 2 {

        let cell2: FlightsInformationCell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as FlightsInformationCell
        cell2.userInteractionEnabled = false

        return cell2
    }

    return cell

}
Run Code Online (Sandbox Code Playgroud)

第0节和第1节正确加载ID为"Cell"的原型单元格,但是当我去加载第2节时,我得到第一个原型单元格的另一个实例减去任何数据,因为它没有被赋予委托或dataSource.否则,单元格的ID分别与"Cell"和"Cell2"相同,但我似乎无法访问"Cell2".

额外澄清:我在故事板中有2个原型单元,它们都设置相同,因为它们都在同一个框中标记了它们的标识符,从它们自己的类继承并在我的UITableView中声明相同.对于delegates和dataSources,我原来的原型单元格中包含一个图形(使用BEMSimpleLineGraph),该单元格的每个实例都有自己的委托和数据源,并在上面的代码中显示了动作0和1.

下图所示的第一个单元格(灰色)是包含图形的原始单元格,而单元格2正好在白色下方.

在此输入图像描述

rde*_*mar 18

我使用类似于你所拥有的代码设置了一个测试应用程序cellForRowAtIndexPath,我得到了相同的结果.即使indexPath.section == 2输入了我的if 子句中的代码,返回的单元格看起来与前两个单元格相同(但标签中没有字符串); 尽管这是事实,我设置了不同的子视图和日志显示它是什么,我想为第2,为什么出现这种情况,我不知道正确的类,但要解决它,你需要什么要做的就是将if块内的单元格出列.

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
}
Run Code Online (Sandbox Code Playgroud)

}

这可以进一步重构以取出重复的代码,但这应该有效......