gpt4 book ai didi

objective-c - UICollectionView header 未显示

转载 作者:IT老高 更新时间:2023-10-28 11:42:22 31 4
gpt4 key购买 nike

我正在开发一个使用 UICollectionView 来显示多个专辑的项目。项目显示正常,但现在我想在第一部分上方显示一个标题。

为此,我将 registerNib:forSupplementaryViewOfKind:withReuseIdentifier: 添加到我的 init 方法中。像这样:

[self.collectionView registerNib:[UINib nibWithNibName:@"AlbumHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kAlbumHeaderIdentifier];

(AlbumHeader Nib 包含 AlbumHeader 类的 View ,它是 UICollectionView 的子类。)

之后,我实现了 collectionView:viewForSupplementaryElementOfKind:atIndexPath 方法:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
return [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:kAlbumHeaderIdentifier forIndexPath:indexPath];
}

我想现在它应该尝试加载标题 View 。但它没有,补充 View 的方法没有被调用。

我错过了什么?坚持了几个小时,已经多次阅读 UICollectionView 上的文档,但似乎没有任何帮助。有什么想法吗?

最佳答案

在寻找 yuf 询问的方法后,我读到默认情况下页眉/页脚的大小为 0,0。如果 size 为 0,则不显示页眉/页脚。

您可以使用属性设置大小:

flowLayout.headerReferenceSize = CGSizeMake(0, 100);

然后所有标题将具有相同的大小。如果每个部分必须不同,您可以实现以下方法,该方法是 UICollectionViewDelegateFlowLayout 协议(protocol)的一部分。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
if (section == albumSection) {
return CGSizeMake(0, 100);
}

return CGSizeZero;
}

请注意,在垂直滚动中它使用返回的 height 和 Collection View 的完整宽度,在水平滚动中它使用返回的 width 和整个高度 Collection View 。

关于objective-c - UICollectionView header 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12700432/

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