美文网首页
使用 UICollectionView 实现瀑布流布局遇到的坑

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

作者: Lorne_coder | 来源:发表于2017-09-19 16:19 被阅读0次

自定义 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;
    ...
 }

相关文章

网友评论

      本文标题:使用 UICollectionView 实现瀑布流布局遇到的坑

      本文链接:https://www.haomeiwen.com/subject/fftgsxtx.html