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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22785507/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:52:54  来源:igfitidea点击:

How to programmatically add header on UICollectionView with UICollectionViewLayout

iosiphonecocoa-touch

提问by Manish Ahuja

I have a UICollectionViewin 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.

我有一个UICollectionView在我的viewcontroller. 我的集合视图使用UICollectionViewLayout(自定义)的子类来布局单元格。首先,只要我在 Storyboard 的下拉列表中选择 Layout 作为 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?

谁能指出我哪里搞砸了?

采纳答案by Manish Ahuja

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

回答by dannyzlo

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。您是否尝试以编程方式添加页眉或页脚?

回答by Sukeshj

Check that you gave reference size for header in UICollectionViewFlowLayout

检查您是否在 UICollectionViewFlowLayout 中为标题提供了参考大小

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

and for footer

和页脚

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

回答by Aaaaaron

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

你的header引用大小的delegate方法不对,你调用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 viewForSupplementaryElementOfKindfor Footer.

单独设置 HeaderReferenceSize 将解决标题问题。但是应用程序会崩溃,您保留上述方法并在页脚的viewForSupplementaryElementOfKind 中返回 nil 。

回答by Attia Mo

I think you should call this in your viewdidloadmethod:

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

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

回答by james075

your check:

你的支票:

if (kind == UICollectionElementKindSectionFooter)

Your should check for: UICollectionElementKindSectionHeader

您应该检查: UICollectionElementKindSectionHeader

same for:

同样适用于:

dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter

dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter