我在UICollectionView
中有两个部分。我想在UICollectionView
中仅显示第1节中的节标题。不在第0部分。
因此,我尝试在nil
:viewForSupplementaryElementOfKind
的方法中返回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:
方法中返回nil也会导致其他人崩溃。其他答案建议删除该方法。
但我想仅显示特定部分的节标题。如何只为一个部分实现返回视图?谢谢。任何帮助将不胜感激。
修改
正如@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;
}
我甚至尝试为sectionHeader设置框架,因为@san说。但没有运气。相同的结果。
答案 0 :(得分:81)
最后,我找到了我的问题的答案。我错过了什么。无论如何,对其他同行用户感到抱歉。
我在下面的方法中设置了标题高度和宽度,直到现在为止@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);
}
}
<强>更新强> 由于有人质疑我的评论技巧,请附上Apple Documentation link以便在上述方法中返回CGSizeZero。
答案 1 :(得分:23)
collectionView:viewForSupplementaryElementOfKind:atIndexPath:
州的文档:
此方法必须始终返回有效的视图对象。如果您不希望在特定情况下使用补充视图,则布局对象不应为该视图创建属性。或者,您可以通过将相应属性的隐藏属性设置为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];
}
答案 2 :(得分:10)
文件清楚地说 -
返回值
配置的补充视图对象。你不能从这个方法返回nil。
所以你需要关注 -
此方法必须始终返回有效的视图对象。如果您不希望在特定情况下使用补充视图,则布局对象不应为该视图创建属性。或者,您可以通过将相应属性的隐藏属性设置为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;
}
答案 3 :(得分:0)
在我的情况下,我将部分插入,因此它给了我空白 因此,如果您已实现以下方法,请按以下方式进行操作
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)
}
}
答案 4 :(得分:-1)
您可以通过添加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;
}
}
所以你可以隐藏/显示它