33

下面的代码正确显示了我的标题视图,但是对于 UICollectionView 中的每个部分:

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
          viewForSupplementaryElementOfKind:(NSString *)kind
                                atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView * headerView =
        [collectionView 
            dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
                               withReuseIdentifier:@"SectionHeaderCollectionReusableView"
                                      forIndexPath:indexPath];
    switch (indexPath.section) {
        case Section_One:
            return headerView;
        case Section_Two:
            return headerView;
        case Section_Three:
            return headerView;
        case Section_Four:
            return headerView;
        case Section_Five:
            return headerView;

        default:
            return headerView;
    }
}

我想做的不是显示“Section_One”或“Section_Two”的标题视图,而是返回“nil”会导致“NSInternalInconsistencyException”:

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
          viewForSupplementaryElementOfKind:(NSString *)kind
                                atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView * headerView =
        [collectionView 
            dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
                               withReuseIdentifier:@"SectionHeaderCollectionReusableView"
                                      forIndexPath:indexPath];
    switch (indexPath.section) {
        case Section_One:
            return nil;
        case Section_Two:
            return nil;
        case Section_Three:
            return headerView;
        case Section_Four:
            return headerView;
        case Section_Five:
            return headerView;

        default:
            return nil;
    }
}

我需要做什么才能仅显示某些部分的标题视图?

4

5 回答 5

57

继续并为每个部分返回一个标题,然后在此 UICollectionViewDelegate 函数中将部分标题的大小设置为零。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeZero;
    }else {
        return CGSizeMake(self.collectionView.bounds.size.width, desiredHeight);
    }
}
于 2014-05-28T15:06:42.800 回答
8

我有一个案例,其中一个UICollectionViewController控制两个UICollectionViews(稍后称为集合视图 1 和 2),我希望第一个的页眉和第二个没有页眉(或页脚)。

@mwright 的回答中缺少的是,当您返回CGSizeZero集合视图 2时,如下所示:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    if collectionView == self.collectionView2 {
        return CGSizeZero
    }
    return < something else >
}

...意味着集合视图 2collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView 根本不会调用。

这意味着您不必担心会徒劳地为第二个集合视图返回“错误”标题。

于 2016-02-19T09:23:06.323 回答
5

我注意到当您在 XIB 中使用 AutoLayout 作为可重用标头时,接受的答案中断了。

如果您将内容与边缘隔开,或者将标题视图中的项目设置为静态的、不可变的大小,则它尤其被破坏。将标头大小设置为 CGSizeZero 使我的调试器控制台变得混乱,其中包含来自 Interface Builder 的数十条警告,说它们会打破所有约束以满足委托方法中设置的要求。

虽然这本身在技术上并不是一场灾难,但它仍然很脏。在 Swift 和 AutoLayout 时代,必须有一个更清洁的解决方案。此外,您永远不想在工作时将这种东西运送给客户。

为了解决这个问题,我不只是调用referenceSizeForHeaderInSection:和返回,而是用 XIBCGSizeZero创建了另一个子类,并将其中的视图高度设置为.UICollectionReusableView0

然后,我在方法switch中包含的语句之外将该变体出列。viewForSupplementaryElementOfKind这满足了 Interface Builder视觉要求!

无论如何,在您调试时,Beats 会在控制台中打印数百个无法满足的约束警告。

于 2016-06-17T02:05:41.400 回答
3

**

If you return a value of size (0, 0), no header will be added.

**

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

 switch section {
 case 0:
   return CGSize(width: collectionView.bounds.width, height: 70)
 case 1:
   return CGSize(width: 0, height: 0) // NO HEADER WILL BE ADDED
 case 2:
   return CGSize(width: collectionView.bounds.width, height: 70)
 case 3:
   return CGSize(width: 0, height: 0) // NO HEADER WILL BE ADDED
 default:
   return CGSize(width: collectionView.bounds.width, height: 70)
 }

}
于 2018-07-01T15:15:27.703 回答
-7

尝试返回一个空视图。可能是更好的方法,但这可以工作....

于 2014-05-28T00:25:55.270 回答