ios 如何以编程方式启用/禁用 UICollectionView 中的节标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24558772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I enable/disable section headers in UICollectionView programmatically?
提问by Xyand
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(复选框)中轻松完成,但是如何在代码中完成呢?
回答by spassas
You can either use the collectionView:layout:referenceSizeForHeaderInSection:
method of the UICollectionViewDelegateFlowLayout
and return CGSizeMake(0,0)
or set accordingly the headerReferenceSize
of UICollectionViewFlowLayout
.
您可以使用collectionView:layout:referenceSizeForHeaderInSection:
的方法UICollectionViewDelegateFlowLayout
,并返回CGSizeMake(0,0)
相应的或设置headerReferenceSize
的UICollectionViewFlowLayout
。
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 文件中添加了相关的行
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,则不会将相应的视图添加到集合视图中。
回答by MiMo
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);
}
}
回答by Hammer
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 页脚视图的高度
回答by Afonso Tsukamoto
When you simply don't want an header to appear, in the delegate's
当您根本不希望出现标题时,请在委托的
viewForSupplementaryElementOfKind
Just return [UIView new];
when kind == UICollectionElementKindSectionHeader:
就return [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*/ ;
}