【问题标题】:UICollectionView Floating Headers on Top and Side顶部和侧面的 UICollectionView 浮动标题
【发布时间】:2013-02-16 01:29:19
【问题描述】:

如何在UICollectionView 中实现标头?我知道您可以添加补充视图,但我不知道如何使它们“浮动”在UITableView 中的标题之类的部分上方。

这是我的情况:我有一个collectionView,其中的单元格以网格格式布置。您可以水平和垂直滚动。我想在顶部有一个标题,这样当您水平滚动时,它会随之水平滚动,但不会垂直滚动。我还希望在左侧有同样的东西,它用collectionView 垂直滚动,但不是水平滚动。使用补充视图来实现这一点是正确的方法吗?

我正在寻找的最终功能类似于 iOS 上的 Numbers 电子表格应用程序。

提前感谢您的帮助!

【问题讨论】:

    标签: objective-c xcode header uicollectionview supplementary


    【解决方案1】:

    编辑:如 cmets 中所述,此答案有效,但不适用于多个部分。请参阅此博客以获得更好的解决方案:http://blog.radi.ws/post/32905838158/sticky-headers-for-uicollectionview-using#notes


    您需要在布局中指定该行为:

    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
    {
      NSMutableArray* attributesArray = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
    
      BOOL headerVisible = NO;
    
      for (UICollectionViewLayoutAttributes *attributes in attributesArray) {
        if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
          headerVisible = YES;
          attributes.frame = CGRectMake(self.collectionView.contentOffset.x, 0, self.headerReferenceSize.width, self.headerReferenceSize.height);
          attributes.alpha = HEADER_ALPHA;
          attributes.zIndex = 2;
        }
      }
    
      if (!headerVisible) {
        UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                                            atIndexPath:[NSIndexPath
                                                                                                         indexPathForItem:0
                                                                                                         inSection:0]];
        [attributesArray addObject:attributes];
      }
    
      return attributesArray;
    }
    

    【讨论】:

    • 那么将框架的 x 原点设置为“self.collectionView.contentOffset.x”会使其保持连接到视图框架的一侧吗?非常感谢您的回答!
    • 在这种情况下,x 轴是正确的。对于您的方案,您需要使用 y 轴之后的第二个标题进行复制。
    • 这是一个破旧的 hack,只适用于只有第一部分的标题的集合视图。
    • @shyambhat:感谢您的关注。请参阅此博客以获得比我更好的答案:blog.radi.ws/post/32905838158/…
    【解决方案2】:

    iOS9 更新:

    let flow = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    flow.sectionHeadersPinToVisibleBounds = true
    

    传下去。

    【讨论】:

    • 快速简单。这是一个比在 UITableView 中选择分组/纯文本更好的选择。谢谢你好先生。
    • 你太棒了!太棒了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 2013-07-05
    • 2012-12-21
    • 1970-01-01
    • 2018-11-02
    相关资源
    最近更新 更多