Use*_*ser 26 uitableview ios swift
IBOutlets连接到故事板中的对象.prototypeCell被命名为我可以使用它dequeueReusableCellWithIdentifier并且它的自定义类属性被设置为commentCell.
第一个错误(我可以解决,但上面的链接都不需要它,这让我觉得我做错了.我是对的吗?):
Overriding method with selector 'initWithStyle:reuseIdentifier:' has incompatible type '(UITableViewCellStyle, String) -> commentCell'
Run Code Online (Sandbox Code Playgroud)
第二个错误(有趣的错误):
'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell'`
Run Code Online (Sandbox Code Playgroud)
细胞类代码:
class commentCell: UITableViewCell {
@IBOutlet weak var authorLabel: UILabel!
@IBOutlet weak var commentLabel: UITextView!
init(style: UITableViewCellStyle, reuseIdentifier: String) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Run Code Online (Sandbox Code Playgroud)
初始化代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
println(comments[indexPath.row])
var cell = self.tableView.dequeueReusableCellWithIdentifier("prototypeCell") as commentCell
cell.commentLabel.text = comments[indexPath.row]["comment"] as NSString
cell.authorLabel.text = comments[indexPath.row]["fromid"] as NSString
return cell
}
Run Code Online (Sandbox Code Playgroud)
rob*_*off 43
第一个初始化程序的正确签名是:
init(style style: UITableViewCellStyle, reuseIdentifier reuseIdentifier: String?)
Run Code Online (Sandbox Code Playgroud)
请注意,这reuseIdentifier是一个Optional,如下所示?.
如果覆盖任何类的指定初始值设定项,则不会继承任何其他指定的初始值设定项.但是UIView采用了NSCoding需要init(coder:)初始化器的协议.所以你也必须实现那个:
init(coder decoder: NSCoder) {
super.init(coder: decoder)
}
Run Code Online (Sandbox Code Playgroud)
但是,请注意,除了调用super之外,您实际上并没有在任何初始化程序中执行任何操作,因此您不需要实现任何初始化程序!如果不覆盖任何指定的初始值设定项,则继承所有超类的指定初始值设定项.
所以我的建议是你只需要init(style:reuseIdentifier:)完全删除初始化程序,除非你要为它添加一些初始化.
如果您计划为其添加一些初始化,请注意故事板中的原型单元格未初始化init(style:reuseIdentifier:).它们由初始化init(coder:).
| 归档时间: |
|
| 查看次数: |
31246 次 |
| 最近记录: |