ios 'required' 初始化程序 'init(coder:)' 必须由 'UITableViewCell' 的子类提供

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26081287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:51:52  来源:igfitidea点击:

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

iosuitableviewswift

提问by User

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

据报道,这段代码在这里这里都有效,但我似乎无法让它工作。

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

IBOutlets 连接到它们在故事板中的对象。prototypeCell 被命名,所以我可以使用它,dequeueReusableCellWithIdentifier并且它的自定义类属性设置为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:

细胞类别代码:

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

Initialization code:

初始化代码:

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
}

回答by rob mayoff

The correct signature for the first initializer is this:

第一个初始化程序的正确签名是这样的:

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

Notice that reuseIdentifieris an Optional, as indicated by the ?.

请注意,reuseIdentifierOptional,如 所示?

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

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

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

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.

但是请注意,除了调用 super 之外,您实际上并没有在任何一个初始化程序中执行任何操作,因此您不需要实现任何一个初始化程序!如果您不覆盖任何指定的初始值设定项,您将继承所有超类的指定初始值设定项。

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(style:reuseIdentifier:)除非您要向其添加一些初始化,否则您只需完全删除初始化程序。

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

如果您打算为其添加一些初始化,请注意故事板中的原型单元不是init(style:reuseIdentifier:). 它们由 初始化init(coder:)

回答by Steve Rosenberg

Not sure why you need a custom UITableViewCell class if you are using storyboard with a prototype cell. You can just drop your labels and textviews into the cell and work with them.

如果您使用带有原型单元格的故事板,不确定为什么需要自定义 UITableViewCell 类。您可以将标签和文本视图放入单元格中并使用它们。

If you are working from a xib then I get it, but you only need:

如果您使用 xib 工作,那么我明白了,但您只需要:

class commentCell: UITableViewCell {
    @IBOutlet weak var authorLabel: UILabel!
    @IBOutlet weak var commentLabel: UITextView!

}

You would then register the xib in the TableView class with:

然后,您可以使用以下命令在 TableView 类中注册 xib:

override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerNib(UINib(nibName: "commentCell", bundle: nil),
            forCellReuseIdentifier: "reuseIdentifier")
    }

Regarding the cellForRowAtIndexPath function, the syntax is now a little modified:

关于 cellForRowAtIndexPath 函数,现在对语法进行了一些修改:

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

 var cell = self.tableView.dequeueReusableCellWithIdentifier("prototypeCell") as commentCell



        return cell
    }

If you want to post to github, we can help make modifications. It's hard to be specific without seeing more of the code.

如果你想发到github,我们可以帮忙修改。如果没有看到更多代码,很难具体说明。

回答by Vladimir Grigorov

The right way to inherit UITableViewCell in Swift 4:

在 Swift 4 中继承 UITableViewCell 的正确方法:

class MyTableViewCell: UITableViewCell
{    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    required init?(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
    }
}

回答by Naishta

Swift 4

斯威夫特 4

Implement the required initas it suggests, and add super.init(nibName: nil, bundle: nil)within your custom initialiser that you are adding

required init按照它的建议实施,并super.init(nibName: nil, bundle: nil)在您要添加的自定义初始化程序中添加

For example:

例如:

init(input: ProtocolType? = nil) {
        super.init(nibName: nil, bundle: nil)
        self.input = input
}

required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
}