集合视图单元格中的标签不会显示

时间:2015-06-02 16:07:50

标签: ios objective-c uicollectionview

我正在尝试让你的标签和图片显示在集合视图单元格中,但只有图像才会出现在我的代码中

标题文件

#import <UIKit/UIKit.h>
#import "ComicMetadata.h"

@interface ComicCell : UICollectionViewCell

@property (nonatomic, retain) UIImageView *page;
@property (nonatomic, retain) UILabel *name;

@end

身体

#import "ComicCell.h"

@implementation ComicCell

- (id)initWithFrame:(CGRect)frame{

self = [super initWithFrame:frame];
if (self){
  self.page = [[UIImageView alloc] init];
  self.name = [[UILabel alloc] initWithFrame:self.bounds];
  self.autoresizesSubviews = YES;

  self.name.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

  self.name.font = [UIFont systemFontOfSize:13];

  self.name.textAlignment = NSTextAlignmentLeft;

  [self.contentView addSubview:self.page];

  [self.contentView addSubview:self.name];

  self.name.text = @"Test";
  }
return self;
}

- (void)layoutSubviews{
  [super layoutSubviews];
  //self.page.frame = self.contentView.bounds;
  self.page.frame = CGRectMake(0, 0, 234, 312);
}

我做错了什么?额外的文本,因为Stackoverflow不喜欢代码与文本的比率

1 个答案:

答案 0 :(得分:0)

当带有框架的ComicCell初始化时,边界不会完全初始化,您必须在layoutSubviews中再次为标签设置框架。

- (void)layoutSubviews{
    [super layoutSubviews];
    self.page.frame = CGRectMake(0, 0, 234, 312);
    self.name.frame = CGRectMake(0, 0, 234, 312);
}