简体   繁体   English

'required'initulator'init(coder :)'必须由'UITableViewCell'的子类提供

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

This code has reportedly worked here and here , but I can't seem to make it work. 据报道,此代码在这里这里都有用,但我似乎无法使其工作。

The IBOutlets are hooked up to their objects in the storyboard. IBOutlets连接到故事板中的对象。 The prototypeCell is named so I can use it with dequeueReusableCellWithIdentifier and it's custom class attribute is set to commentCell . 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
}

The correct signature for the first initializer is this: 第一个初始化程序的正确签名是:

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

Notice that reuseIdentifier is an Optional , as indicated by the ? 请注意, reuseIdentifier是一个Optional ,如? .

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. 但是UIView采用了NSCoding协议,它需要一个init(coder:)初始化器。 So you must implement that one too: 所以你也必须实现那个:

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! 但是,请注意,除了调用super之外,您实际上并没有在任何初始化程序中执行任何操作,因此您不需要实现任何初始化程序! If you don't override any designated initializers, you inherit all of your superclass's designated initializers. 如果不覆盖任何指定的初始值设定项,则继承所有超类的指定初始值设定项。

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 not initialized by init(style:reuseIdentifier:) . 如果您计划为其添加一些初始化,请注意故事板中的原型单元格不会init(style:reuseIdentifier:) They are initialized by init(coder:) . 它们由init(coder:)初始化。

Not sure why you need a custom UITableViewCell class if you are using storyboard with a prototype cell. 如果您将故事板与原型单元格一起使用,则不确定为什么需要自定义UITableViewCell类。 You can just drop your labels and textviews into the cell and work with them. 您可以将标签和文本视图放入单元格并使用它们。

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. 如果您想发布到github,我们可以帮助您进行修改。 It's hard to be specific without seeing more of the code. 没有看到更多代码就很难具体。

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

Swift 4 斯威夫特4

Implement the required init as 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")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift:错误:“必需”初始值设定项“init(coder:)”必须由“UIView”的子类提供 - Swift : Error: 'required' initializer 'init(coder:)' must be provided by subclass of 'UIView' 'required'initulator'init(coder :)'必须由UIViewController的子类提供 - 'required' initializer 'init(coder:)' must be provided by subclass of UIViewController Swift:错误:“必填”初始化程序“ init(coder :)”必须由“ NSURLRequest”的子类提供 - Swift: Error : “required' initializer 'init(coder:)' must be provided by subclass of 'NSURLRequest'” 为什么我必须继续为我的编程 UIViewController 子类声明相同的必需但未实现的初始化程序(编码器 aDecoder)? - Why must I keep declaring the same required but not implemented initializer for init(coder aDecoder) for my programatic UIViewController subclass? 它显示“必须在UIControl的子类中提供必需的初始化程序初始化”当我重写init(frame:CGRect)时 - It shows “required initializer init must be provided in subclass of UIControl” When I override init(frame: CGRect) UIViewController和必需的init?(编码器:) - UIViewController and required init?(coder: ) 自定义UIView子类,使用实例vars时需要实现init(coder :) - Custom UIView subclass, required to implement init(coder:) when using instance vars Swift如何避免重写init? - Swift how to avoid override init?(coder) in subclass 致命错误:对类使用未实现的初始化程序 'init(coder:)' - Fatal error: use of unimplemented initializer 'init(coder:)' for class 显示错误非故障初始化器“ init(coder :)”错误 - Shows an error Non - fail able initializer “init(coder:)” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM