73

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.

2
  • This may be a dumb suggestion, but did you try cleaning and rebuilding? Xcode can sometimes continue to appear to have the error if you used its auto-complete suggestion. Commented Aug 16, 2016 at 4:20
  • Yes I've cleaned and restarted. No avail. Commented Aug 16, 2016 at 4:24

2 Answers 2

180

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   
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hey that worked thanks! Removed the "fatalError("init(coder:) has not been implemented")" and replaced with "super.init(coder: aDecoder)". And yes, you're correct that the cell should still work without the inits, but I wanted to add a gesture recognizer to the cell when it's initialized. Thanks again!
This is great and it works but would someone able to explain why changing initializer worked?
@JamesRyu in question's example, initializer contain fatalError("init(coder:) has not been implemented") line, which caused the crash when that initializer get called.
Am I missing something? I’m getting “Property .<Class>. not initialized at super.init call.
@ScottyBlades looks like one of your instance properties not initialized before calling super.init.
13

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.