3

I'm trying to subclass a UITableViewCell in Swift.

I've tried this:

class CUISwitchTableViewCell: UITableViewCell {

    var label = UILabel()
    var switchControl = UISwitch()

    init(coder aDecoder: NSCoder!) {
        super.init(coder: aDecoder)

        // Set up UI
    }
}

But I get a compiler error when calling super.init(coder: aDecoder):

Must call a designated initializer of the superclass 'UITableViewCell'

So what I understand is that I must call init(style: UITableViewCellStyle, reuseIdentifier: String!), but then I will lose all the setups made in Interface Builder. If I not overriding initWithCoder where should I set up my views?

1 Answer 1

8

If you want to setup your views, or perform any custom initialisation of your objects that have been archived using interface builder, you can do so by overriding awakeFromNib. Every object that has been created using interface builder will get this method called when it is unarchived from Nib. And overriding this method will also keep your interface builder setups intact.

Sign up to request clarification or add additional context in comments.

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.