如何使用UICollectionViewLayout以编程方式在UICollectionView上添加标头

[英]How to programmatically add header on UICollectionView with UICollectionViewLayout


I have a UICollectionView in one of my viewcontroller. My collection view uses a subclass of UICollectionViewLayout (custom) to layout the cells. First thing, as soon as I select Layout as Custom in dropdown on Storyboard, option to select supplementary views goes away.

我的一个viewcontroller中有一个UICollectionView。我的集合视图使用UICollectionViewLayout(自定义)的子类来布局单元格。首先,只要在Storyboard的下拉列表中选择Layout as Custom,选择补充视图的选项就会消失。

I tried doing that programatically as shown below, but none of the delegate methods are getting called.

我尝试以编程方式执行此操作,如下所示,但没有调用任何委托方法。

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {

        UICollectionReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

        if (reusableview==nil) {
            reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
          }

        UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        label.text=[NSString stringWithFormat:@"Recipe Group #%li", indexPath.section + 1];
        [reusableview addSubview:label];
        return reusableview;
      }
    return nil;
  }

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    CGSize headerSize = CGSizeMake(320, 44);
    return headerSize;
  }

In my viewDidLoad Method I have

在我的viewDidLoad方法中我有

[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];

Can anyone point me where I'm messing up?

任何人都可以指出我搞砸了哪里?

6 个解决方案

#1


18  

You're passing in the incorrect view kind.

你传递的是错误的视图类型。

Your line registering the class:

你注册班级的线路:

[self.collectionView registerClass:[UICollectionReusableView class]
  forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
  withReuseIdentifier:@"HeaderView"];

Should be:

应该:

[self.collectionView registerClass:[UICollectionReusableView class]
  forSupplementaryViewOfKind: UICollectionElementKindSectionHeader
  withReuseIdentifier:@"HeaderView"];

Edit: Looks like your code is all using sectionFooter. Are you trying to programmatically add a header or a footer?

编辑:看起来您的代码全部使用sectionFooter。您是否尝试以编程方式添加页眉或页脚?

#2


9  

Found the issue, I was not returning attributes of my header in this UICollectionLayoutView method:

发现问题,我没有在此UICollectionLayoutView方法中返回标头的属性:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect; // return an array layout attributes instances for all the views in the given rect

#3


8  

Check that you gave reference size for header in UICollectionViewFlowLayout

检查您是否在UICollectionViewFlowLayout中为标题指定了引用大小

[flowLayout setHeaderReferenceSize:CGSizeMake(320, 50)];

and for footer

并为页脚

[flowLayout setFooterReferenceSize:CGSizeMake(320, 50)];

#4


1  

Your delegate method for header reference size is wrong, you call footer method,referenceSizeForFooterInSection, as follow:

你的头文件引用大小的委托方法是错误的,你调用footer方法,referenceSizeForFooterInSection,如下:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
  CGSize headerSize = CGSizeMake(320, 44);
  return headerSize;
}

Individually set HeaderReferenceSize will fix the header problem. But app will crash you keep the above method and return nil in viewForSupplementaryElementOfKind for Footer.

单独设置HeaderReferenceSize将修复标头问题。但app会崩溃你保留上面的方法并在viewForSupplementaryElementOfKind for Footer中返回nil。

#5


1  

I think you should call this in your viewdidload method:

我想你应该在你的viewdidload方法中调用它:

[collectionViewFlowLayout setHeaderReferenceSize:CGSizeMake(self.collectionView.frame.size.width, 50)];

#6


0  

your check:

你的支票:

if (kind == UICollectionElementKindSectionFooter)

Your should check for: UICollectionElementKindSectionHeader

您应该检查:UICollectionElementKindSectionHeader

same for:

同样的:

dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter

dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2014/04/01/6f81e7516295106c437a54b6d7cdbec4.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告