gpt4 book ai didi

ios - 如何使用 UICollectionViewLayout 以编程方式在 UICollectionView 上添加 header

转载 作者:IT王子 更新时间:2023-10-29 07:59:11 27 4
gpt4 key购买 nike

我的一个 viewcontroller 中有一个 UICollectionView。我的 Collection View 使用 UICollectionViewLayout(自定义)的子类来布置单元格。首先,一旦我在 Storyboard 的下拉菜单中选择 Layout 作为 Custom,选择补充 View 的选项就会消失。

我尝试如下所示以编程方式执行此操作,但没有调用任何委托(delegate)方法。

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if (kind == UICollectionElementKindSectionHeader) {

UICollectionReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

if (reusableview==nil) {
reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
}

UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
label.text=[NSString stringWithFormat:@"Recipe Group #%li", indexPath.section + 1];
[reusableview addSubview:label];
return reusableview;
}
return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
CGSize headerSize = CGSizeMake(320, 44);
return headerSize;
}

在我的 viewDidLoad 方法中有

[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];

谁能指出我哪里搞砸了?

最佳答案

您传递的 View 种类不正确。

你注册类(class)的线路:

[self.collectionView registerClass:[UICollectionReusableView class]
forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
withReuseIdentifier:@"HeaderView"];

应该是:

[self.collectionView registerClass:[UICollectionReusableView class]
forSupplementaryViewOfKind: UICollectionElementKindSectionHeader
withReuseIdentifier:@"HeaderView"];

编辑:看起来您的代码都在使用 sectionFooter。您是否尝试以编程方式添加页眉或页脚?

关于ios - 如何使用 UICollectionViewLayout 以编程方式在 UICollectionView 上添加 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22785507/

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