我有两个部分 UICollectionView.我想显示一个节标题在 UICollectionView 只有第一节。不在第0节。

所以我试着回来 nilviewForSupplementaryElementOfKind:的方法 section == 0 并返回视图 section == 1.

它崩溃并显示以下错误:

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

这里是我的补充视图的代码。

- (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: 方法崩溃的其他人了。其他答案建议删除该方法。

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

编辑:

正如@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;
}

我甚至尝试像@san所说的那样为sectionHeader设置框架。但运气不好。同样的结果。

有帮助吗?

解决方案

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

我按照@san所说的那样,在下面的方法中设置了标题高度和宽度。

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

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

这对我来说真的很有效:

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

更新资料: 由于有人质疑我的评论技巧,附上苹果 文档链接 用于在上述方法中返回CGSizeZero。

其他提示

有关文件 collectionView:viewForSupplementaryElementOfKind:atIndexPath: 国家:

此方法必须始终返回有效的视图对象。如果您不希望在特定情况下使用补充视图,则布局对象不应为该视图创建属性。或者,您可以通过将相应属性的hidden属性设置为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];
}

文件清楚地说 -

返回值

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

所以你需要跟随 -

此方法必须始终返回有效的视图对象。如果您不希望在特定情况下使用补充视图,则布局对象不应为该视图创建属性。或者,您可以通过将相应属性的hidden属性设置为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;
}

在我的情况下,我已经给了插入部分,所以它给了我空的空间 所以,如果你已经实现了以下方法,请按照以下方式执行

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        if <condition for which you want to hide section>{
            return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        }else{
            return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        }
    }

您可以通过添加隐藏/显示可重用的标题部分 UICollectionViewDelegateFlowLayout 委托和使用下面的代码

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
if (self.isForSearch) { //---> for hiding
    return CGSizeMake(0,0);
}
else{//---> for showing
    return ((UICollectionViewFlowLayout*)self.collectionChoosePlanView.collectionViewLayout).headerReferenceSize;
}
}

所以你可以隐藏/显示它

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top