本文介绍了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