簡體   English   中英

initWithCoder未設置UILabel屬性(UIFont和文本顏色)

[英]initWithCoder doesn't set UILabel property (UIFont and text color)

我的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;
    }

但由於某種原因,它沒有設置媒體資源。 我必須將其移出init方法,並將其放在單獨的方法中,並在調用后調用此方法:

 dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath

然后它會工作。 知道為什么嗎?

您想在awakeFromNib:做這些事情awakeFromNib:當筆尖中的所有內容均已實際加載並連接時

從文檔獲取NSObject UIKit添加

討論
筆尖加載基礎結構向從筆尖存檔重新創建的每個對象發送awakeFromNib消息,但是僅在存檔中的所有對象均已加載並初始化之后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM