首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将多个原型单元加载到UITableView

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

Stack Overflow用户
提问于 2015-01-17 06:01:15
回答 1查看 16.8K关注 0票数 12

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

代码语言:javascript
复制
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。否则,单元格的设置将分别与“cells”和"Cell2“的ID相同,但我似乎无法访问"Cell2”。

额外的说明:我的故事板中有两个原型单元,它们的设置是相同的,因为它们的标识符都在相同的框中标记,继承自它们自己的类,并且在我的UITableView中被声明为相同的。至于委托和dataSources,我最初的原型单元格包含一个图(使用BEMSimpleLineGraph),该单元格的每个实例都有它自己的委托和数据源,并在上面的代码中显示了操作0和1。

下图中的第一个单元格(灰色)是包含图形的原始单元格,cell2的下方是白色。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-17 08:39:22

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

代码语言:javascript
复制
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
}

}

这可以进一步重构,以删除重复的代码,但这应该是可行的……

票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27993496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档