gpt4 book ai didi

ios - UICollectionView 使用不同高度的单元格

转载 作者:可可西里 更新时间:2023-11-01 06:19:41 27 4
gpt4 key购买 nike

所以我有一个包含 3 个单元格的 Collection View Controller ,因为每个单元格都有不同的布局。我的图片显示了这一点:

enter image description here

它们都有不同的单元格标识符,因此在我的 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;
}

但是当我模拟它时,所有单元格的大小都相同:

enter image description here

有没有办法强制单元格为我定义的大小,或者有更合适的方法吗?

最佳答案

试试这个:Custom layout for different cell sizes in UICollectionView

或者使用这个方法:

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

关于ios - UICollectionView 使用不同高度的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19520040/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com