本文介绍了UICollectionView 每行显示 3 个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从情节提要创建的 UICollectionView,

I have a UICollectionView created from storyboard,

我想在视图中每行有 3 个项目.我设法使用以下方法做到了这一点:

I want to have 3 items per row in the view. I managed to do that using the following:

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(collectionView.frame.size.width/3.6 , (collectionView.frame.size.height-100)/2);
}

但是,如果我有 6 个项目,我会得到 2 行,每行 3 个项目.但是当我有 4 个项目时,它将显示 2 行,每行 2 个项目.

However, if i have 6 items, i got 2 rows with 3 items each.But when i have 4 items, it will display 2 rows with 2 items each.

是否可以将它们显示为 2 行,第一行包含 3 个项目,第二行仅包含 1 个?

Is it possible to display them in 2 rows, the first with 3 items and the second contains only 1?

推荐答案

我知道已经很晚了,但是我目前正在使用并处理此案例的解决方案是使用 KRLCollectionViewGridLayout,如下所示

I know it is so late, but the solution that i am currently using and handled this case was by using KRLCollectionViewGridLayout as below

        KRLCollectionViewGridLayout* aFlowLayout = [[KRLCollectionViewGridLayout alloc]init];
        aFlowLayout.aspectRatio = 1;
        aFlowLayout.sectionInset = UIEdgeInsetsMake(2, 2, 2, 2);
        aFlowLayout.interitemSpacing = 2;
        aFlowLayout.lineSpacing = 2;
        aFlowLayout.numberOfItemsPerLine = 3;
        aFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;

        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.heigh) collectionViewLayout:aFlowLayout];

您可以使用此链接

这篇关于UICollectionView 每行显示 3 个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 12:14