简体   繁体   English

UICollectionView使用不同高度的单元格

[英]UICollectionView using cells with different heights

So I have a collection view controller that has 3 cells because each cell will have a different layout. 所以我有一个集合视图控制器,它有3个单元格,因为每个单元格都有不同的布局。 My image shows this: 我的图片显示了这个:

在此输入图像描述

They all have different cell identifiers so in my cellForItemAtIndexPath function I can create the specific cell like: 它们都有不同的单元格标识符,因此在我的cellForItemAtIndexPath函数中,我可以创建特定的单元格,如:

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


    if(indexPath.row == 0)
    {
        static NSString *identifier = @"Cell1";
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier     forIndexPath:indexPath];
        [cell setNeedsLayout];
    }
    else if(indexPath.row == 1)
    {
        static NSString *identifier = @"Cell2";
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        [cell setNeedsLayout];
    }
    else
    {
        static NSString *identifier = @"Cell3";
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        [cell setNeedsLayout];
    }

    return cell;
}

But when I simulate it, all the cells come out the same size: 但是当我模拟它时,所有单元格的大小都相同:

在此输入图像描述

Is there a way to force the cell to be the size I have defined or is there a more appropriate way of doing this? 有没有办法强制单元格达到我定义的大小,或者有更合适的方法吗?

Try this : Custom layout for different cell sizes in UICollectionView 试试这个: UICollectionView中不同单元格大小的自定义布局

or use this method : 或使用此方法:

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

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

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