致命错误:init(编码器:)尽管已实现但尚未实现错误 [英] fatal error: init(coder:) has not been implemented error despite being implemented

查看:450
本文介绍了致命错误:init(编码器:)尽管已实现但尚未实现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一条错误消息:


致命错误:init(编码器:)尚未实现

fatal error: init(coder:) has not been implemented

我的自定义 UITableViewCell 。单元格未注册,故事板中具有标识符单元格以及使用dequeasreusablecell时。在自定义单元格中,我有以下内容:

For my custom UITableViewCell. The cell is not registered, has the identifier cell in the storyboard and when using dequeasreusablecell. In the custom cell I have the inits as:

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    print("test")
    super.init(style: style, reuseIdentifier: reuseIdentifier)
}

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

但我仍然有错误。谢谢。

But I still have the error. Thanks.

推荐答案

用编码器方法替换你的init:

Replace your init with coder method:

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

实际上如果你在Storyboard中创建了你的单元格 - 我相信它应该附加到你尝试创建它的tableView上。如果你没有在那里执行任何逻辑,你可以删除你的两个init方法。

Actually if you have your cell created in Storyboard - I believe that it should be attached to tableView on which you try to create it. And you can remove both of your init methods if you do not perform any logic there.

UPD
如果你需要添加任何逻辑 - 你可以在awakeFromNib()方法中执行此操作。

UPD: If you need to add any logic - you can do this in awakeFromNib() method.

override func awakeFromNib() {
   super.awakeFromNib()
   //custom logic goes here   
}

这篇关于致命错误:init(编码器:)尽管已实现但尚未实现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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