如果节标题隐藏在UICollectionView中,则删除空白空间 [英] Removing empty space, if the section header is hidden in the UICollectionView

查看:400
本文介绍了如果节标题隐藏在UICollectionView中,则删除空白空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UICollectionView 中有两个部分。我想在第一部分的 UICollectionView 中显示一个节头。不在第0部分。

I have two sections in UICollectionView. I want to show a section header in UICollectionView for only 1st section. Not in 0th section.

所以我试图在 viewForSupplementaryElementOfKind nil c $ c>:的方法= = 0 并返回部分的视图== 1

So I tried to return nil in viewForSupplementaryElementOfKind: method for section == 0 and returns view for the section == 1.

它崩溃并显示以下错误:

It crashes and shows below error:

Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes]:

这是我的补充视图代码。

Here it is my code for the supplementary view.

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

我发现在中返回nil viewForSupplementaryElementOfKind:方法也崩溃了。其他答案建议删除该方法。

I have found that returning nil in viewForSupplementaryElementOfKind: method crashing for others too. Other answers suggesting to remove that method.

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

But I want to show section header for specific sections only. How to achieve that returning view for only one section? Thanks. Any help would be appreciated.

编辑:

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

As @san said, I have updated code to hide the section header. It works. It hides the header. But I am still seeing the empty space in the place of section header. Expected results is there should be no space for section header, if it is hidden.

更新代码:

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

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

推荐答案

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

At last, I found an answer for my question. I have missed something. Anyway sorry for other fellow users.

我在下面的方法中设置了标题高度和宽度,直到现在为止@san说。

I set the header height and width inside the below method till now as @san said.

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

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

But It is not the correct method to set the frame size of supplementary views. Later I found another method inside the flowLayout, which helps me to set the header and footer sizes.

这对我来说非常有用:

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

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

UPDATE: Since someone questioned about my skill in comments, attaching Apple Documentation link for returning CGSizeZero in above method.

这篇关于如果节标题隐藏在UICollectionView中,则删除空白空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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