使用 UICollectionView 實現(xiàn)瀑布流布局遇到的坑

自定義 cell 繼承于 UICollectionViewCell 的時候

1.初始化,在這里可以做一些添加子視圖的操作,注意:僅僅是添加子視圖,不要在這里進行子視圖的布局操作:

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

  1. 子視圖的 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;
    ...
 }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容