自定義 cell 繼承于 UICollectionViewCell 的時候
1.初始化,在這里可以做一些添加子視圖的操作,注意:僅僅是添加子視圖,不要在這里進行子視圖的布局操作:
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self.contentView addSubview:self.imageView];
}
return self;
}
- 子視圖的 frame 是基于 cell 的 bounds 來布局的,在給 cell 填充數(shù)據(jù)時,進行子視圖的布局操作
錯誤寫法1:
- (void)setModel:(WaterfallModel *)model
{
...
self.imageView.frame = self.contentView.frame;
...
}
錯誤寫法2:
- (void)setModel:(WaterfallModel *)model
{
...
self.imageView.frame = self.contentView.bounds;
...
}
錯誤寫法3:
- (void)setModel:(WaterfallModel *)model
{
...
self.imageView.frame = self.frame;
...
}
正確寫法:
- (void)setModel:(WaterfallModel *)model
{
...
self.imageView.frame = self.bounds;
...
}