Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

ios8

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?

like image 259
Gustaf Rosenblad Avatar asked Feb 22 '26 15:02

Gustaf Rosenblad


1 Answers

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.

like image 144
salman140 Avatar answered Feb 24 '26 13:02

salman140