0

Is it advisable to set UIFont of a UILabel in layoutSubviews. I am subclassing a UITableViewCell. We use layoutSubview to adjust the UIView frame if required, so I am not sure whether I should go with it.

Also, is there is method like viewDidLoad, which will be called only once.

I tired to set the font in initWithCoder, but IBOutlet of UILabel is empty in initWithCoder.

I can set the UILabel text in cellForRowAtIndexPath, so connection to this custom UILabel is correct.

Note:- The cell is initialized from a UIViewController in UIStoryboard.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
weber67
  • 525
  • 1
  • 9
  • 18

1 Answers1

1

You should only layout your views in layoutSubviews. UITableViewCell is a subclass of UIView, so you can set up your properties here:

- (void) awakeFromNib {
    self.myLabel.text = @"my text";    
}
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Thanks. Do I need to call [super awakeFromNib], before I edit any properties of UILable like text or font. – weber67 Jul 11 '13 at 17:43
  • Good question. You don't need to call super in this case. You can see some example code here, although some stuff is Mac-specific: https://developer.apple.com/library/mac/#documentation/cocoa/Reference/ApplicationKit/Protocols/NSNibAwaking_Protocol/Reference/Reference.html – Aaron Brager Jul 11 '13 at 19:00
  • Thanks, url helped me to understand my query. Call to super is not required if the parent class is UIView or NSObject. For others if the parent class does not implement -(void) awakeFromNib, it will throw a exception. To avoid this one can perform a check using instancesRespondToSelector:. – weber67 Jul 12 '13 at 13:36