UICollectionView目标C中有2种不同的像元大小 [英] 2 different cell sizes in UICollectionView Objective C

查看:104
本文介绍了UICollectionView目标C中有2种不同的像元大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用UICollectionView,可以创建以下布局:

I would like to use UICollectionView, it is possible to create the following layout:

如您所见,单元有两种不同的大小。一个是行的1/4,另一个是行的3/4。可以使用UICollectionView创建这种布局吗?

As you can see, there are two different sizes of the cell. One is 1/4 of the row, the other is 3/4. Is it possible to create this kind of layout with a UICollectionView?

任何人都可以教我该怎么做吗???还是有样品吗???我已经学习了许多教程和参考。但还是不知道该怎么做。

Would anyone can teach me how to do it??? Or is there any samples??? I have already study many tutorials and reference. But still dont know how to do it..

谢谢!

推荐答案

好吧,我暂时对项目宽度进行了硬编码(72.0和23.0)。 5.0的其余部分将用于临时间距和edgeInsets中。此代码将为您提供所需的确切信息。

Well I have hardcoded the item width for the time being (72.0 and 23.0). The rest of 5.0 will be used in interim spacing and edgeInsets. This code will give you exactly what you want.

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10.0;
}

#pragma mark - CollectionViewFlowLayout Methods

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize newSize = CGSizeZero;
    newSize.height = 100;

    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBounds.size;

    if(indexPath.item % 4 == 0 || indexPath.item % 4 == 3)
    {
        // Size : 1/4th of screen
        newSize.width = screenSize.width * 0.23;
    }
    else
    {
        // Size : 3/4th of screen
        newSize.width = screenSize.width * 0.72;

    }
    return newSize;
}

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

这篇关于UICollectionView目标C中有2种不同的像元大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆