如何以编程方式在UICollectionView中启用/禁用节标题?

时间:2022-08-25 23:40:21

How can I enable/disable section headers in UICollectionView programmatically?

如何以编程方式在UICollectionView中启用/禁用节标题?

It can be easily done easily done in Storyboard (checkbox), but how about doing it in code?

它可以在Storyboard(复选框)中轻松完成,但是如何在代码中完成呢?

5 个解决方案

#1


31  

You can either use the collectionView:layout:referenceSizeForHeaderInSection: method of the UICollectionViewDelegateFlowLayout and return CGSizeMake(0,0) or set accordingly the headerReferenceSize of UICollectionViewFlowLayout.

您可以使用UICollectionViewDelegateFlowLayout的collectionView:layout:referenceSizeForHeaderInSection:方法并返回CGSizeMake(0,0)或相应地设置UICollectionViewFlowLayout的headerReferenceSize。

Edit: headerReferenceSize is actually the property that storyboard uses to show/hide the headers. I've added the relevant lines from the Storyboard file

编辑:headerReferenceSize实际上是storyboard用于显示/隐藏标题的属性。我添加了Storyboard文件中的相关行

With section checkbox on:

选中部分复选框:

 <collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="xAt-Uo-bMl">
           <size key="headerReferenceSize" width="50" height="50"/></collectionViewFlowLayout>

With section checkbox off

关闭部分复选框

 <collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="xAt-Uo-bMl">
           <size key="headerReferenceSize" width="0" height="0"/></collectionViewFlowLayout>

Edit #2:

编辑#2:

From the official docs:

来自官方文档:

Each section in a flow layout can have its own custom header and footer. To configure the header or footer for a view, you must configure the size of the header or footer to be non zero. You can do this by implementing the appropriate delegate methods or by assigning appropriate values to the headerReferenceSize and footerReferenceSize properties. If the header or footer size is 0, the corresponding view is not added to the collection view.

流布局中的每个部分都可以拥有自己的自定义页眉和页脚。要为视图配置页眉或页脚,必须将页眉或页脚的大小配置为非零。您可以通过实现适当的委托方法或将适当的值分配给headerReferenceSize和footerReferenceSize属性来完成此操作。如果页眉或页脚大小为0,则相应的视图不会添加到集合视图中。

#2


9  

Just change the height to 0 of the headers you don't want to show...

只需将高度更改为您不想显示的标题的0 ...

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeZero;
    }else {
        return CGSizeMake(collectionView.frame.size.width,50);
    }
}

#3


1  

Neither nil nor [UIView new] works an both throw the same error. The best answer is in How to change the UICollectionView footerview's height programatically

nil和[UIView new]都没有同样的错误。最佳答案是如何以编程方式更改UICollectionView footerview的高度

#4


-2  

When you simply don't want an header to appear, in the delegate's

如果您只是不希望在委托中出现标题

viewForSupplementaryElementOfKind

Just return [UIView new]; when kind == UICollectionElementKindSectionHeader:

只需返回[UIView new];当kind == UICollectionElementKindSectionHeader时:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{   
    if (kind == UICollectionElementKindSectionHeader) {
       return [UIView new]; // Or even nil, I think it would work.
    }
    ...
    return /*something else that you want to return*/ ;
}

#5


-2  

There is a property named "isHidden" and it can be used to hide the section header when not needed.

有一个名为“isHidden”的属性,可用于在不需要时隐藏节标题。

Please refer to the below code:

请参考以下代码:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    var reusableView : UICollectionReusableView!
    if (kind == UICollectionElementKindSectionHeader) {
        let cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderCell", for: indexPath) as! CVHeader
        let arrayData = summary[indexPath.section]
        cell.headerLabel.text = arrayData.first as? String
        if cell.headerLabel.text == "" {
            cell.isHidden = true //this will hide the header and leave a blank space.
        }
        reusableView = cell
    }
    if (kind == UICollectionElementKindSectionFooter) {
        reusableView = nil
    }

    return reusableView
}

The code above hides the sectional header if the text is empty. This works perfectly fine on xcode8.

如果文本为空,则上面的代码隐藏了截面标题。这在xcode8上运行得非常好。

#1


31  

You can either use the collectionView:layout:referenceSizeForHeaderInSection: method of the UICollectionViewDelegateFlowLayout and return CGSizeMake(0,0) or set accordingly the headerReferenceSize of UICollectionViewFlowLayout.

您可以使用UICollectionViewDelegateFlowLayout的collectionView:layout:referenceSizeForHeaderInSection:方法并返回CGSizeMake(0,0)或相应地设置UICollectionViewFlowLayout的headerReferenceSize。

Edit: headerReferenceSize is actually the property that storyboard uses to show/hide the headers. I've added the relevant lines from the Storyboard file

编辑:headerReferenceSize实际上是storyboard用于显示/隐藏标题的属性。我添加了Storyboard文件中的相关行

With section checkbox on:

选中部分复选框:

 <collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="xAt-Uo-bMl">
           <size key="headerReferenceSize" width="50" height="50"/></collectionViewFlowLayout>

With section checkbox off

关闭部分复选框

 <collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="xAt-Uo-bMl">
           <size key="headerReferenceSize" width="0" height="0"/></collectionViewFlowLayout>

Edit #2:

编辑#2:

From the official docs:

来自官方文档:

Each section in a flow layout can have its own custom header and footer. To configure the header or footer for a view, you must configure the size of the header or footer to be non zero. You can do this by implementing the appropriate delegate methods or by assigning appropriate values to the headerReferenceSize and footerReferenceSize properties. If the header or footer size is 0, the corresponding view is not added to the collection view.

流布局中的每个部分都可以拥有自己的自定义页眉和页脚。要为视图配置页眉或页脚,必须将页眉或页脚的大小配置为非零。您可以通过实现适当的委托方法或将适当的值分配给headerReferenceSize和footerReferenceSize属性来完成此操作。如果页眉或页脚大小为0,则相应的视图不会添加到集合视图中。

#2


9  

Just change the height to 0 of the headers you don't want to show...

只需将高度更改为您不想显示的标题的0 ...

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeZero;
    }else {
        return CGSizeMake(collectionView.frame.size.width,50);
    }
}

#3


1  

Neither nil nor [UIView new] works an both throw the same error. The best answer is in How to change the UICollectionView footerview's height programatically

nil和[UIView new]都没有同样的错误。最佳答案是如何以编程方式更改UICollectionView footerview的高度

#4


-2  

When you simply don't want an header to appear, in the delegate's

如果您只是不希望在委托中出现标题

viewForSupplementaryElementOfKind

Just return [UIView new]; when kind == UICollectionElementKindSectionHeader:

只需返回[UIView new];当kind == UICollectionElementKindSectionHeader时:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{   
    if (kind == UICollectionElementKindSectionHeader) {
       return [UIView new]; // Or even nil, I think it would work.
    }
    ...
    return /*something else that you want to return*/ ;
}

#5


-2  

There is a property named "isHidden" and it can be used to hide the section header when not needed.

有一个名为“isHidden”的属性,可用于在不需要时隐藏节标题。

Please refer to the below code:

请参考以下代码:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    var reusableView : UICollectionReusableView!
    if (kind == UICollectionElementKindSectionHeader) {
        let cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderCell", for: indexPath) as! CVHeader
        let arrayData = summary[indexPath.section]
        cell.headerLabel.text = arrayData.first as? String
        if cell.headerLabel.text == "" {
            cell.isHidden = true //this will hide the header and leave a blank space.
        }
        reusableView = cell
    }
    if (kind == UICollectionElementKindSectionFooter) {
        reusableView = nil
    }

    return reusableView
}

The code above hides the sectional header if the text is empty. This works perfectly fine on xcode8.

如果文本为空,则上面的代码隐藏了截面标题。这在xcode8上运行得非常好。