Din*_*aja 40 iphone ios uicollectionview uicollectionreusableview ios7
我有两个部分UICollectionView
.我想UICollectionView
仅显示第1节中的节标题.不在第0部分.
于是,我就回nil
以viewForSupplementaryElementOfKind
:方法section == 0
并返回查看的section == 1
.
它崩溃并显示以下错误:
Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes]:
Run Code Online (Sandbox Code Playgroud)
这是补充视图的代码.
- (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;
}
Run Code Online (Sandbox Code Playgroud)
我发现在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;
}
Run Code Online (Sandbox Code Playgroud)
@san说,我甚至尝试为sectionHeader设置框架.但没有运气.同样的结果.
Din*_*aja 82
最后,我找到了我的问题的答案.我错过了什么.无论如何对不起其他同行用户.
@san说,我在下面的方法中设置了标题高度和宽度.
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
但这不是设置补充视图的帧大小的正确方法.后来我在flowLayout中找到了另一个方法,它帮助我设置页眉和页脚大小.
这对我来说非常有用:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return CGSizeZero;
}else {
return CGSizeMake(CGRectGetWidth(collectionView.bounds), 135);
}
}
Run Code Online (Sandbox Code Playgroud)
更新: 由于有人质疑我的评论技巧,附上Apple 文档链接以返回上述方法中的CGSizeZero.
Cap*_*uff 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];
}
Run Code Online (Sandbox Code Playgroud)
San*_*San 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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
25433 次 |
最近记录: |