【问题标题】:UICollectionView header not showingUICollectionView 标头未显示
【发布时间】:2012-10-03 00:40:46
【问题描述】:

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

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

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

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

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

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

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

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

【问题讨论】:

    标签: objective-c cocoa-touch uicollectionview


    【解决方案1】:

    在寻找 yuf 询问的方法后,我读到默认情况下页眉/页脚的大小为 0,0。如果 size 为 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 和集合视图的完整高度。

    【讨论】:

    • 对于仍然遇到此问题的任何人,为了让我完成这项工作,我还必须为“collectionview.footerReferenceSize”指定一个大小。不确定该请求是否特定于 iOS 7...
    • 这是一个令人难以置信的提示。谢谢一百万,Guido!
    • 享受赏金吧! :)
    • @JoeBlow 谢谢,感激!
    • 不是给定一个 100px 的高度,是否可以找到集合标题视图的实际高度并将其返回?
    【解决方案2】:

    你实现了吗:

    - (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

    【讨论】:

    • 该方法似乎不是任何委托的一部分?
    • @GuidoH 您使用的是 FlowLayout 还是您的自定义布局?这个答案是指自定义布局。
    • 我确实使用了流式布局。接受的答案解决了这一切,也很有意义。 =)
    【解决方案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,我也从他们的回答中得到了启示

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      相关资源
      最近更新 更多