开发者

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 = [sup开发者_运维问答er 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜