[英]initWithCoder doesn't set UILabel property (UIFont and text color)
I have the following code for my UICollectionViewCell 我的UICollectionViewCell有以下代码
@property (strong, nonatomic) IBOutlet UIImageView *storyImage;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UILabel *descriptionLabel;
@end
@implementation Story
@synthesize storyImage;
@synthesize titleLabel;
@synthesize descriptionLabel;
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self.contentView addSubview:self.titleLabel];
[self.contentView addSubview:self.descriptionLabel];
[self.contentView addSubview:self.storyImage];
[self.titleLabel setFont:[UIFont fontWithName:kProximaNovaBold size:15]];
[self.descriptionLabel setFont:[UIFont fontWithName:kProximaNova size:13]];
[self.descriptionLabel setTextColor:[UIColor colorWithWhite:140/255.f alpha:1.0]];
self.descriptionLabel.frame = CGRectIntegral(self.descriptionLabel.frame);
self.descriptionLabel.center = roundedCenterPoint(self.descriptionLabel.center);
}
return self;
}
but for some reason it's not setting up the property. 但由于某种原因,它没有设置媒体资源。 I'd have to move it out of the init method and put it inside a separate method and call this after calling:
我必须将其移出init方法,并将其放在单独的方法中,并在调用后调用此方法:
dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath
and then it would work. 然后它会工作。 Any idea why?
知道为什么吗?
You want to be doing this stuff in awakeFromNib:
when everything from the nib has actually been loaded and connected 您想在
awakeFromNib:
做这些事情awakeFromNib:
当笔尖中的所有内容均已实际加载并连接时
From the docs for NSObject UIKit Additions 从文档获取NSObject UIKit添加
Discussion
讨论
The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized.笔尖加载基础结构向从笔尖存档重新创建的每个对象发送awakeFromNib消息,但是仅在存档中的所有对象均已加载并初始化之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.