微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我目前有一个含有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

}

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

附加说明:我的故事板中有2个原型单元格,它们都设置相同,因为它们的标识符都标注在相同的框中,继承自己的类,并在我的UITableView中被声明为相同.对于delegate和dataSources,我的原始原型单元格保存一个图形(使用BEMSimpleLineGraph),这个单元格的每个实例都有自己的委托和数据源,并在上面的代码显示了动作0和1.

下图(灰色)中的第一个单元格是保存图形的原始单元格,单元格2在白色正下方.

解决方法

我使用与您在cellForRowAtIndexPath中具有的代码类似的代码设置了一个测试应用程序,我得到相同的结果.即使输入了我的ifPathPath.section == 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
}

}

这可以进一步重构以取代重复的代码,但这应该工作…

原文地址:https://www.jb51.cc/iOS/336058.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐