如果节标题隐藏在UICollectionView中,则删除空白空间

Din*_*aja 40 iphone ios uicollectionview uicollectionreusableview ios7

我有两个部分UICollectionView.我想UICollectionView仅显示第1节中的节标题.不在第0部分.

于是,我就回nilviewForSupplementaryElementOfKind:方法section == 0并返回查看的section == 1.

它崩溃并显示以下错误:

Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes]:
Run Code Online (Sandbox Code Playgroud)

这是补充视图的代码.

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *sectionHeader = nil;
    if (kind == UICollectionElementKindSectionHeader && indexPath.section == 1) {
        sectionHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"EventSectionHeader" forIndexPath:indexPath];
        sectionHeader.layer.borderWidth = .5f;
        sectionHeader.layer.borderColor = [UIColor colorWithRed:221.0 / 255.0 green:223.0 / 255.0 blue:220.0 / 255.0 alpha:1.0].CGColor;
    }

    return sectionHeader;
}
Run Code Online (Sandbox Code Playgroud)

我发现在viewForSupplementaryElementOfKind:方法崩溃时也会返回nil .其他答案建议删除该方法.

但我想只显示特定部分的节标题.如何只为一个部分实现返回视图?谢谢.任何帮助,将不胜感激.

编辑:

正如@san所说,我已更新代码以隐藏节标题.有用.它隐藏了标题.但我仍然看到节标题处的空白空间.预期结果是如果隐藏了节标题,则应该没有空格.

更新的代码:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

    UICollectionReusableView *sectionHeader = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        sectionHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"EventSectionHeader" forIndexPath:indexPath];
        sectionHeader.layer.borderWidth = .5f;
        sectionHeader.layer.borderColor = [UIColor colorWithRed:221.0 / 255.0 green:223.0 / 255.0 blue:220.0 / 255.0 alpha:1.0].CGColor;
        if (indexPath.section == 0) {
            sectionHeader.hidden = YES;

        }else {
            sectionHeader.hidden = NO;
        }
    }

    return sectionHeader;
}
Run Code Online (Sandbox Code Playgroud)

@san说,我甚至尝试为sectionHeader设置框架.但没有运气.同样的结果.

Din*_*aja 82

最后,我找到了我的问题的答案.我错过了什么.无论如何对不起其他同行用户.

@san说,我在下面的方法中设置了标题高度和宽度.

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

但这不是设置补充视图的帧大小的正确方法.后来我在flowLayout中找到了另一个方法,它帮助我设置页眉和页脚大小.

这对我来说非常有用:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeZero;
    }else {
        return CGSizeMake(CGRectGetWidth(collectionView.bounds), 135);
    }
}
Run Code Online (Sandbox Code Playgroud)

更新: 由于有人质疑我的评论技巧,附上Apple 文档链接以返回上述方法中的CGSizeZero.

  • 仅供参考,`CGSizeMake(0,0)`可以简化为`CGSizeZero`.恭喜找到答案! (9认同)
  • 不幸的是,同样的方法对我不起作用.我正在检查某个部分是否为空,标题没有按预期显示,但是每个空格都添加了大约20px ... (3认同)

Cap*_*uff 23

collectionView:viewForSupplementaryElementOfKind:atIndexPath:各州的文件:

此方法必须始终返回有效的视图对象.如果您不希望在特定情况下使用补充视图,则布局对象不应为该视图创建属性.或者,您可以通过将相应属性的隐藏属性设置为YES或将属性的alpha属性设置为0来隐藏视图.要在流布局中隐藏页眉和页脚视图,还可以设置这些视图的宽度和高度到0.

考虑到您已经尝试将高度设置为零并将视图设置为隐藏,您应该进行子类化UICollectionViewFlowLayout和实现layoutAttributesForSupplementaryViewOfKind:atIndexPath:

检查indexPath(如您所做)并返回,nil如果您不想要该特定补充视图的任何布局属性.

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if([indexPath section] == 0)
    {
         return nil;
    }

    return [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
}
Run Code Online (Sandbox Code Playgroud)

  • 当我将子流布局子类化时,甚至没有调用此方法.我想我仍然缺少一些东西.无论如何,我已经添加了一个答案.非常感谢队长的时间. (2认同)

San*_*San 10

文件清楚地说 -

回报价值

配置的补充视图对象.您不能从此方法返回nil.

所以你需要跟随 -

此方法必须始终返回有效的视图对象.如果您不希望在特定情况下使用补充视图,则布局对象不应为该视图创建属性.或者,您可以通过将相应属性的隐藏属性设置为YES或将属性的alpha属性设置为0来隐藏视图.要在流布局中隐藏页眉和页脚视图,还可以设置这些视图的宽度和高度到0.

来到你的代码,下面的代码片段应该适合你:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *sectionHeader = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        sectionHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"EventSectionHeader" forIndexPath:indexPath];

        if(indexPath.section == 1)
          {
             sectionHeader.layer.borderWidth = .5f;
             sectionHeader.layer.borderColor = [UIColor colorWithRed:221.0 / 255.0 green:223.0 / 255.0 blue:220.0 / 255.0 alpha:1.0].CGColor;
          }
        else
        {
          sectionHeader.frame = CGRectZero;
          sectionHeader.hidden = YES;
        }
    }

    return sectionHeader;
}
Run Code Online (Sandbox Code Playgroud)

  • 我已经完全做到了..没有任何改进..仍然在节标题 0 中显示空白。 (2认同)