UICollectionView 标头未显示 [英] UICollectionView header not showing

查看:48
本文介绍了UICollectionView 标头未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a project that uses an UICollectionView to show several albums. The items show fine, but now I want to show an header above the first section.

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

To do this, I added the registerNib:forSupplementaryViewOfKind:withReuseIdentifier: to my init method. Like this:

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

(AlbumHeader Nib 包含 AlbumHeader 类的视图,它是 UICollectionView 的子类.)

(The AlbumHeader Nib contains a view of the class AlbumHeader, which is a subclass of UICollectionView.)

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

After that, I implemented collectionView:viewForSupplementaryElementOfKind:atIndexPath method:

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

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

Now it should try to load the header view, I suppose. But it doesn't, the method for the supplementary view doesn't get called.

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

What am I missing? Stuck for hours, have read the documentation on UICollectionViews many times, but nothing seems to help. Any thoughts?

推荐答案

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

After looking for the method yuf asked about, I read that by default the size of headers/footers are 0,0. If the size is 0, the header/footer won't display.

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

You can set the size with a property:

flowLayout.headerReferenceSize = CGSizeMake(0, 100);

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

Then all the headers will have the same size. If it has to be different for each section, you can implement the following method, which is part of the UICollectionViewDelegateFlowLayout protocol.

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

    return CGSizeZero;
}

请注意,在垂直滚动中它使用返回的 height 和集合视图的完整宽度,在水平滚动中它使用返回的 width 和整个高度集合视图.

Note that in vertical scrolling it uses the returned height and the full width of the collection view, in horizontal scrolling it uses the return width and the full height of the collection view.

这篇关于UICollectionView 标头未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆