繁体   English   中英

UICollectionView具有不同单元格的自动布局

[英]UICollectionView auto layout with different cells

我使用UICollectionView与自动布局显示画廊。 我有2种prototype cells -一种显示为2列的collectionView,另一种显示为3列的视图。

单击按钮后,视图将切换为2列和3列

以下是我的代码:

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellGrid" forIndexPath:indexPath];
        if([selected_layout isEqualToString:@"tile"]){
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellTile" forIndexPath:indexPath];  // for 2 columns
        } else {
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellGrid" forIndexPath:indexPath];  // for 3 columns
        }
[cell layoutIfNeeded];
    [cell setNeedsDisplay];

    return cell;
}

我没有在代码中的任何地方设置单元格的高度/宽度,我只是在xib中指定了它。 但是每次显示3列的collectionView ,每个单元格之间都有大约10像素的黑色间隙。

我引用了此链接,但它使用的是swift和自定义单元格类。 我不想用那个。

我该如何解决? 我哪里出问题了?

添加以下委托方法:

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{


        return 5;


}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(20, 10, 10, 10);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM