ios 致命错误:init(coder:) 尽管已实现但尚未实现错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38966565/
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
fatal error: init(coder:) has not been implemented error despite being implemented
提问by Lee
I am receiving an error message:
我收到一条错误消息:
fatal error: init(coder:) has not been implemented
致命错误:init(coder:) 尚未实现
For my custom UITableViewCell
. The cell is not registered, has the identifier cellin the storyboard and when using dequeasreusablecell. In the custom cell I have the inits as:
对于我的习惯UITableViewCell
。该单元格未注册,在情节提要中以及使用 dequeasreusablecell 时具有标识符单元格。在自定义单元格中,我将初始化设置为:
Code:
代码:
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.
但我仍然有错误。谢谢。
回答by ruslan.musagitov
Replace your init with coder method:
用编码器方法替换你的 init:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
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.
实际上,如果您在 Storyboard 中创建了单元格 - 我相信它应该附加到您尝试在其上创建它的 tableView。如果您不在那里执行任何逻辑,则可以删除两个 init 方法。
UPD: If you need to add any logic - you can do this in awakeFromNib() method.
UPD:如果您需要添加任何逻辑 - 您可以在awakeFromNib() 方法中执行此操作。
override func awakeFromNib() {
super.awakeFromNib()
//custom logic goes here
}
回答by Natasha
Firstly, you need to call the super class' init(coder:)
method with the statement super.init(coder: aDecoder)
. You do that by adding
it right under the method signature like-
首先,您需要init(coder:)
使用语句调用超类的方法super.init(coder: aDecoder)
。您可以通过在方法签名下添加它来做到这一点,例如 -
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
Secondly, you need to remove the statement,
其次,您需要删除语句,
fatalError("init(coder:) has not been implemented")
.
fatalError("init(coder:) has not been implemented")
.
That should work.
那应该工作。