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

查看:30
本文介绍了删除空白空间,如果部分标题隐藏在 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:section == 0 的方法并返回 section == 的视图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;
}

我发现在 viewForSupplementaryElementOfKind: 方法中返回 nil 也会对其他人造成崩溃.建议删除该方法的其他答案.

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

我什至尝试像@san 所说的那样为 sectionHeader 设置框架.但没有运气.相同的结果.

推荐答案

我的问题终于找到了答案.我错过了一些东西.无论如何,对不起其他用户.

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.

这对我来说真的很管用:

This really works well for me:

- (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天全站免登陆