Subclassified UITableViewCell not initialized when using loadNibNamed

I have a class that subclasses UITableViewCell. I need to initialize some of the values ​​in the cell when it is created, and I create it using:

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

I get the following method when subclassing through 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 executed. How can i do this? If this is not the method used to create the object (using NSBundle loadNibNamed), then what is it? How can I initialize a cell when it is created from nib this way?

Any help is greatly appreciated.

+4
source share
2 answers

When loading from nib, views are initialized by calling initWithCoder: rather than one of your usual initialization methods. You can override this method to perform initialization, or you could implement awakeFromNib .

+7
source

Nib loading does not create any objects since they are already created and then serialized in the nib file.

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

0
source

Source: https://habr.com/ru/post/1347023/


All Articles