将多个原型单元加载到UITableView中 [英] Load Multiple Prototype Cells into UITableView

查看:92
本文介绍了将多个原型单元加载到UITableView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 with no success. My cellForRowAtIndexPAth method is as follows:

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节时,我得到第一个原型单元格的另一个实例减去任何数据,因为它没有被赋予代表或数据源。否则,单元格的ID设置与Cell和Cell2相同,但我似乎无法访问Cell2。

Section 0 and Section 1 correctly load the prototype cell with ID "Cell" but when I go to load section 2 I get another instance of the first prototype cell less any data as it hasn't been assigned a delegate or dataSource. Otherwise the cells are set up identically with ID's of "Cell" and "Cell2" respectively but I can't seem to access "Cell2".

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

Additional Clarification: I have 2 prototype cells in my storyboard, they are both set up the same in that they both have their identifiers labeled in the same boxes, inherit from their own classes and have been declared the same in my UITableView. As for the delegates and dataSources, my original prototype cell holds a graph (uses BEMSimpleLineGraph), each instance of this cell has it's own delegate and datasource and shown in the above code for actions 0 and 1.

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

The first cell pictured below (in grey) is the original cell that holds a graph and cell2 is just below it in white.

推荐答案

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

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...

这篇关于将多个原型单元加载到UITableView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆