Gustaf Rosenblad
Gustaf Rosenblad

Reputation: 1922

Override init(coder aDecoder: NSCoder!) not working like expected - Swift

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?

Upvotes: 3

Views: 5038

Answers (1)

salman140
salman140

Reputation: 1510

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.

Upvotes: 8

Related Questions