Why initialization of UITableViewCell does not work inside initWithCoder

I am trying to clear my code and use the principles of MVC by clicking on as many viewing-related materials as I can in the storyboard, as well as on custom UIView classes (i.e. instead of doing things related to viewing on UIViewController)

So, I have a custom UITableViewCell (called CustomCell) that has several properties, one of which is my own label. Since I load a cell from the storyboard, I initialize it initWithCoder, and not initWithStyle:reuseIdentifier:, this is what I have in CustomCell.m, which is a subclass UITableViewCell(for some reason I could not figure out how to set a custom font using the storyboard .. but this does not apply to this question):

// CustomCell.m - subclass of UITableViewCell
- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        NSLog(@"customizing cell font having text %@", self.label.text);
        UIFont *customFont = [UIFont fontWithName:@"Montserrat" size:16];
        self.label.textColor = [UIColor redColor];
        [self.label setFont:customFont];
    }
    return self;
}

. null , b/c . self.label ( , , nib ), .. .

, , TableViewController:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath {        
    UIFont *customFont = [UIFont fontWithName:@"Montserrat" size:16];
    [((CustomCell *)cell).label setFont:customFont];
}

. , , CustomCell.m

update:, . UITableViewCell initWithCoder, ! :

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        UIView *bgColorView = [[UIView alloc] init];
        [bgColorView setBackgroundColor:[UIColor blueColor]];
        [self setSelectedBackgroundView:bgColorView];  // actually works!
    }
    return self;
}

.

+4
2

andykkt, awakeFromNib :

nib-load awakeFromNib , nib, , . awakeFromNib, , .

: initWithCoder UITableViewCell ( backgroundView .. ..). , .. awakeFromNib..

+7

, initWithCoder: , , , , ui "" , . UIViewController initWithNibName:bundle:.

0

All Articles