我下载了我的变量,但我无法调用新类的功能

时间:2015-05-30 00:59:38

标签: ios uitableview swift

我在iOS Swift项目中创建了一些原型单元格,我现在尝试根据表单如何使用单元格来初始化单元格。

这是细胞的样子:

enter image description here

我现在正在尝试创建表单,使其以两个公开单元格(右侧有“Detail>”的单元格)开头,然后是一个带开关的单元格。

这些原型中的每一个都与他们自己的各个类相关联。

以下是我如何生成细胞:

var cell : UITableViewCell?
    switch indexPath.section {
    case 0:
        switch indexPath.row {
        case 0,1:
            cell = tableView.dequeueReusableCellWithIdentifier("reportCellWithDisclosureIndicator", forIndexPath: indexPath) as! reportCellWithDisclosureIndicator
        case 2:
            cell = tableView.dequeueReusableCellWithIdentifier("reportCellWithSwitch", forIndexPath: indexPath) as! reportCellWithSwitch
        default:
            cell = tableView.dequeueReusableCellWithIdentifier("reportCellWithDisclosureIndicator", forIndexPath: indexPath) as! reportCellWithDisclosureIndicator
        }
    default:
        cell = tableView.dequeueReusableCellWithIdentifier("reportCellWithDisclosureIndicator", forIndexPath: indexPath) as! reportCellWithDisclosureIndicator
    }

...
switch indexPath.row {
case 0:
    cell?.textLabel!.text = "Medications"
    cell?.detailTextLabel!.text = "None"
case 1:
    cell?.textLabel!.text = "Allergies"
    cell?.detailTextLabel!.text = "None"
case 2:
if(cell is reportCellWithSwitch) {
   cell?.configure("Smoking")    // PROBLEM LINE HERE
} ...

函数configurereportCellWithSwitch类的成员。我遇到的问题是当我尝试配置特定的单元格时。

这是configure类中的reportCellWithSwitch函数:

import UIKit

public class reportCellWithSwitch: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var cellSwitch: UISwitch!
    public func configure(text: String) {
        // This serves to replace the title in the prototype cell
        titleLabel!.text = text
    }
    ...
}

如何使用configure功能?

我是否必须为每个细胞类型声明特定的个体变量?

我应该编写一个包含所有配置并根据需要调用函数的通用类吗?

任何想法和见解都将非常感激。谢谢!

1 个答案:

答案 0 :(得分:1)

if(cell is reportCellWithSwitch) {
   let  castcell = cell as! reportCellWithSwitch
   castcell.configure("Smoking")    
}

您应首先将单元格(UITableviewCell)强制转换为castcell(reportCellWithSwitch),因为UITableviewCell不具有configure

的功能