4

I don't know if this is specific to UITableViewCells but rather general for UIViews (as i believe) but i noticed the problem with a cell.

As mentioned, I have a custom UITableViewCell subclass which loads itself from a xib when it gets initialized with an designated init:

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil];
    self = nib[0];
    if (self) {
        // Initialization code
        _reuseIdentifier = reuseIdentifier;
    }
    return self;
}

It has a UILabel as an outlet and I set some properties of it in the awakeFromNib method:

- (void)awakeFromNib{
    [super awakeFromNib];

    self.labelLeft.textColor = [UIColor grayColor];
    self.labelLeft.font = [UIFont boldSystemFontOfSize:15.0f];
}

The point is, that the label doesn't keep the textColor nor the font and I don't understand why.
awakeFromNib gets called and the outlet is connected right, since i can set the text.

I can make it work by settings those properties after I set his text in the UITableViewDataSource but I don't feel it's the right way and I want to understand why this doesn't work.

Question:
Why doesn't it keep the Font and Textcolor and what can I do to make it work the right way?

yinkou
  • 5,756
  • 2
  • 24
  • 40
  • "which loads itself from a xib" - here's the problem. I bet if you created the cell programmatically, it would work. –  Dec 19 '12 at 16:51
  • Yeah, if i would have it done all by code it definitely would have worked. But i want to know why this doesn't work. – yinkou Dec 19 '12 at 16:53
  • Well it kinda works. It just doesn't keep the color and Font... – yinkou Dec 19 '12 at 16:54
  • "If there is no answer after 30 min. on StackOverflow, there is no solution to your problem." - quote anonymous da internez. – yinkou Dec 19 '12 at 17:24
  • 1
    I have the same issue. Yes, I can setting new font and color in every update method of the cell, but I wont to set it only once. – ArtFeel Sep 12 '13 at 09:34
  • I haven't solved it yet either. – yinkou Sep 12 '13 at 10:01
  • 1
    I found where the problem was. Try to explain more detail in the answer. – ArtFeel Sep 13 '13 at 15:39

2 Answers2

2

I have the same issue with all my labels loaded from .Nib's. Then I found that the problem was in UIAppearance. I have the following line in my AppDelegate:

+ (void)styleApplication {
    [[UILabel appearance] setFont:[MGStylesheet defaultLightFontOfSize:17]];
}

This method update all fonts in my application, and all works fine, until I start loading my UIView's from .Nib. When I removed this, everything start working as expected.

ArtFeel
  • 11,701
  • 4
  • 29
  • 41
  • Yeah of course UIAppearance! Just put the code in `-layoutSubviews` after you call super and it works. Makes totaly sense! – yinkou Sep 15 '13 at 13:14
-1

Set the attributes of the label in the nib-file. If you split your interface configuration to different places this causes just trubble.

If you don't want to do that move the configuration of the label into viewDidLoad:

HackingOtter
  • 134
  • 6