HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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),这个单元格的每个实例都有自己的委托和数据源,并在上面的代码中@L_874_10@了动作0和1.

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

解决方法

我使用与您在cellForRowATindexPath中具有的代码类似的代码设置了一个测试应用程序,我得到相同的结果.即使输入了我的ifPathPath.section == 2子句中的代码,返回的单元格与前两个单元格相同(但标签中没有字符串);这是尽管我设置了不同的子视图,一个日志@L_874_10@它是我想要的部分的正确的类
为什么会发生这种情况,我不知道,但要@L_616_20@这个问题,你需要做的就是将单元格出现在你的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
}

}

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

大佬总结

以上是大佬教程为你收集整理的ios – 将多个原型单元格加载到UITableView中全部内容,希望文章能够帮你解决ios – 将多个原型单元格加载到UITableView中所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。