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

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

问题描述

我收到一条错误消息:

致命错误:init(coder:) 尚未实现

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

对于我的自定义 UITableViewCell.单元格未注册,在故事板和使用 dequeasreusablecell 时具有标识符 cell.在自定义单元格中,我将初始化设置为:

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.

推荐答案

用编码器方法替换你的初始化:

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(coder:) 尽管已实现但尚未实现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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