我在我的 iOS Swift 项目中创建了一些原型(prototype)单元格,我现在正尝试根据表单使用单元格的方式来初始化单元格。
这是细胞的样子:
我现在正在尝试创建表单,使其以两个披露单元格(右侧有“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
} ...
函数configure
是reportCellWithSwitch
类的成员。我遇到的问题是当我尝试配置该特定单元格时。
这是 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/