thephatp
thephatp

Reputation: 1489

Subclassed UITableViewCell not being initialized when using loadNibNamed

I have a class that subclasses UITableViewCell. I need to initialize some of the values in the cell when it gets created, and I'm creating it using:

NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];

I get the following method when creating the subclass via the XCode "Add New File" interface:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) 
    {
        self.selectionStyle = UITableViewCellSelectionStyleBlue;

        [txtField setEnabled:YES];
        [txtField setTextColor:[UIColor redColor]];
        [txtField setPlaceholder:@"Fake Placeholder - Test Initialize"];

        [contactBtn setEnabled:YES];
    }
    return self;
}

The code in this method is never getting executed. How can I make that happen? If this isn't the method that is used when instantiating the object (using NSBundle loadNibNamed), then what is? How can I initialize the cell when creating it from the nib this way?

Any help is greatly appreciated.

Upvotes: 4

Views: 1718

Answers (2)

Terry Wilcox
Terry Wilcox

Reputation: 9040

Loading the nib doesn't create any objects as they're already created then serialized in the nib file.

Try the awakeFromNib method, which gets sent to all objects in the nib file.

Upvotes: 0

Anomie
Anomie

Reputation: 94804

When loaded from a nib, views are initialized by calling initWithCoder: rather than one of your normal initialization methods. You could override this method to do your initialization, or you could implement awakeFromNib.

Upvotes: 7

Related Questions