【发布时间】:2016-02-03 10:38:40
【问题描述】:
我有一个继承UITableViewCell 的基类,它声明了一个变量来存储类型值,该类型值将在init 和其他函数中使用:
class CellBase: UITableViewCell {
var type: Int
init(theType: Int) {
type = theType
if (type == 100) { // more init code based on 'type'
} else {
}
}
// other functions
}
我有继承CellBase的子类:
class Cell1: CellBase {
init() {
super.init(type: 100)
}
}
class Cell2: CellBase {
init() {
super.init(type: 200)
}
}
我想将type 的值传递给CellBase。但是我想不出办法来做到这一点。 XCode 显示一些错误:
'required'初始化器'init(coder:)'必须由'UITableViewCell'的子类提供
如果我修改代码以覆盖所需的初始化程序:
class CellBase2 : UITableViewCell {
var type: Int
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我无法将类型值从子类传递给CellBase。如何解决这个问题?
【问题讨论】:
-
如果单元格是从 NIB 中解压缩的,您将如何提供类型?你可以吗?因此
fatal error... -
@Wain 我没有使用 NIB。所有视图都是以编程方式创建的。我的问题不在于
fatalError。谢谢。 -
重点是您已经有了解决问题的代码
标签: swift