'required'initulator'init(coder :)'必须由'UITableViewCell'的子类提供 [英] 'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell'`

查看:130
本文介绍了'required'initulator'init(coder :)'必须由'UITableViewCell'的子类提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据报道,此代码已在此处这里,但我似乎无法使其正常工作。

This code has reportedly worked here and here, but I can't seem to make it work.

IBOutlets与故事板中的对象相连。
prototypeCell被命名为,所以我可以将它与 dequeueReusableCellWithIdentifier 一起使用,并且它的自定义类属性设置为 commentCell

The IBOutlets are hooked up to their objects in the storyboard. The prototypeCell is named so I can use it with dequeueReusableCellWithIdentifier and it's custom class attribute is set to commentCell.

第一个错误(我可以解决,但上面的链接都不需要它,这让我觉得我做错了。我是对的吗?):

First Error (which I can solve, but neither of the links above needed it, which makes me think I'm doing something wrong. Am I right?):

Overriding method with selector 'initWithStyle:reuseIdentifier:' has incompatible type '(UITableViewCellStyle, String) -> commentCell'

第二个错误(有趣的错误):

Second Error (the interesting error):

'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell'`

Cell Class Code:

Cell Class Code:

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)
    }
}

初始化代码:

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
}


推荐答案

第一个初始值设定项的正确签名是:

The correct signature for the first initializer is this:

init(style style: UITableViewCellStyle, reuseIdentifier reuseIdentifier: String?)

请注意, reuseIdentifier 可选,如下所示

Notice that reuseIdentifier is an Optional, as indicated by the ?.

如果覆盖任何类的指定初始值设定项,则不会继承任何其他指定的初始值设定项。但 UIView 采用 NSCoding 协议,该协议需要 init(编码器:) 初始化程序。所以你也必须实现那个:

If you override any of a class's designated initializers, you don't inherit any other designated initializers. But UIView adopts the NSCoding protocol, which requires an init(coder:) initializer. So you must implement that one too:

init(coder decoder: NSCoder) {
    super.init(coder: decoder)
}

但请注意,你实际上并没有做任何事情初始化器除了调用super之外,所以你不需要实现任何初始化器!如果你没有覆盖任何指定的初始值设定项,你继承了所有超类的指定初始值设定项。

Note, however, that you're not actually doing anything in either initializer except calling super, so you don't need to implement either initializer! If you don't override any designated initializers, you inherit all of your superclass's designated initializers.

所以我的建议是你只需删除 init (样式:reuseIdentifier:)完全初始化,除非你要为它添加一些初始化。

So my advice is that you just remove your init(style:reuseIdentifier:) initializer entirely unless you're going to add some initialization to it.

如果你打算添加对它进行一些初始化,请注意故事板中的原型单元格 init(样式:reuseIdentifier:)初始化。它们由 init(编码器:) 初始化。

And if you're planning to add some initialization to it, be advised that prototype cells in a storyboard are not initialized by init(style:reuseIdentifier:). They are initialized by init(coder:).

这篇关于'required'initulator'init(coder :)'必须由'UITableViewCell'的子类提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆