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

IosSwiftUitableview

Ios Problem Overview


I am receiving an error message: > fatal error: init(coder:) has not been implemented

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:

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.

Ios Solutions


Solution 1 - Ios

Replace your init with coder method:

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.

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

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

Solution 2 - Ios

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-

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

Secondly, you need to remove the statement,

fatalError("init(coder:) has not been implemented").

That should work.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeeView Question on Stackoverflow
Solution 1 - Iosruslan.musagitovView Answer on Stackoverflow
Solution 2 - IosNatashaView Answer on Stackoverflow