46

我正在做一个项目,该项目使用一个UICollectionView来显示几张专辑。这些项目显示得很好,但现在我想在第一部分上方显示一个标题。

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

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

AlbumHeaderNib 包含类的视图AlbumHeader,它是 的子类UICollectionView。)

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

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

现在它应该尝试加载标题视图,我想。但事实并非如此,补充视图的方法不会被调用。

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

4

3 回答 3

123

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

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

flowLayout.headerReferenceSize = CGSizeMake(0, 100);

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

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

    return CGSizeZero;
}

请注意,在垂直滚动中,它使用height集合视图的返回和全宽,在水平滚动中,它使用集合视图的返回width和全高。

于 2012-10-03T09:49:10.680 回答
3

您是否实施:

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

有很多方法可以实现只是为了让一件事起作用……我也在学习。告诉我它是否有效。

编辑:对不起错误的方法。那是我认为的子类化。我正在谈论的是UICollectionViewLayout(如果您的布局支持补充视图,您的子类的布局对象):

- layoutAttributesForSupplementaryViewOfKind:atIndexPath:

见这里:https ://developer.apple.com/library/ios/#documentation/UIKit/Reference/UICollectionViewLayout_class/Reference/Reference.html#//apple_ref/occ/cl/UICollectionViewLayout

于 2012-10-03T01:09:03.723 回答
3

适用于SWIFT 3 和 SWIFT 4

    self.collectionView.register(UINib(nibName: "SectionCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SectionCollectionReusableView")



    self.collectionView.fs_width = self.collectionView.bounds.width

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 70, left: 40, bottom: 0, right: 0)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    layout.estimatedItemSize = CGSize(width: 350, height: 140)
    layout.scrollDirection = .horizontal
    layout.headerReferenceSize = CGSize(width: self.collectionView.bounds.size.width, height: 60)
    layout.sectionHeadersPinToVisibleBounds = true
    self.collectionView!.collectionViewLayout = layout

您必须将此添加到ViewDidLoad,并注意

layout.headerReferenceSize = CGSize(width: self.collectionView.bounds.size.width, height: 60)

这将使标题部分布局

并感谢@Guido Hendriks,我也从他们的回答中获得了洞察力

于 2019-01-15T19:38:27.977 回答