简体   繁体   English

在UICollectionView中连续显示不同数量的单元格

[英]Display different amount of cells in a row within a UICollectionView

I would like to have a UICollectionView that, in the first row there is a cell with all the row's width, and the second row has 3 cells. 我想要一个UICollectionView ,在第一行中有一个具有所有行宽度的单元格,第二行有3个单元格。 I have researched the documentation but I'm not able to do it. 我已经研究了文档,但是无法做到。 Something like this: 像这样:

在此处输入图片说明

Thanks in advance 提前致谢

Make 2 Cell prototypes in collection view in your storyboard, and set them reuse identifiers 在情节提要的集合视图中制作2个Cell原型,并将其设置为重用标识符

In your ViewController's .m file implement this functions 在ViewController的.m文件中实现此功能

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{        
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell;

    if (/*condition for small cell*/)// e.g.    indexPath.row % 3 != 0
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"smallCell" forIndexPath:indexPath];
    else
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bigCell" forIndexPath:indexPath];


    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (/*condition for small cell*/) // e.g.    indexPath.row % 3 != 0
        return CGSizeMake(65, 50);

    return CGSizeMake(318, 50);
}

After doing this you will have something like this: 完成此操作后,您将获得以下内容:

在此处输入图片说明

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

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