如果部分标题隐藏在 UICollectionView 中,则删除空白区域 [英] Removing empty space, if the section header is hidden in the 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屋!