使用 UICollectionView 实现瀑布流布局遇到的坑

自定义 cell 继承于 UICollectionViewCell 的时候

1.初始化,在这里可以做一些添加子视图的操作,注意:仅仅是添加子视图,不要在这里进行子视图的布局操作:

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self.contentView addSubview:self.imageView];
    }
    return self;
}

  1. 子视图的 frame 是基于 cell 的 bounds 来布局的,在给 cell 填充数据时,进行子视图的布局操作

错误写法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;
    ...
 }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容