ios - 我向下转换了我的变量,但我无法调用新类的函数

标签 ios uitableview swift

我在我的 iOS Swift 项目中创建了一些原型(prototype)单元格,我现在正尝试根据表单使用单元格的方式来初始化单元格。

这是细胞的样子:

enter image description here

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

这些原型(prototype)中的每一个都链接到它们自己的单独类。

下面是我如何生成单元格:

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 类的成员。我遇到的问题是当我尝试配置该特定单元格时。

这是 reportCellWithSwitch 类中的 configure 函数:

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函数?

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

我是否应该编写一个通用类来保存我的所有配置并根据需要调用函数?

任何想法和见解将不胜感激。谢谢!

最佳答案

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

你应该先将单元格(UITableviewCell)转换为单元格(reportCellWithSwitch),因为UITableviewCell没有configure的功能

关于ios - 我向下转换了我的变量,但我无法调用新类的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30541132/

相关文章:

android - Xamarin iOS 和 Android 中的 ConcurrentExclusiveSchedulerPair 失败

ios - 验证 Facebook/Twitter 连接的最佳实践。 (防止逆向工程 key )

iphone - UITableViewCell 动画

ios - 如何在 segue 中获取选定的 TableView 单元格行?

ios - 如何在 Swift 中将 int 值附加到字符串数组中?

插入时 Swift 4 + SQLite

ios - 调用 .cancel() 时 DispatchWorkItem 不终止函数

ios - 如何使用 tabBarItems 在 ipad actionSheet 中显示 pickerView

ios - Swift:在警报 View 中按下按钮后,tableView 不会重新加载数据

ios - 除了加载行的子集之外,使用 NSFetchedResultsController 有什么好处