gpt4 book ai didi

ios - 未调用 UICollectionReusableView 方法

转载 作者:IT王子 更新时间:2023-10-29 07:32:51 24 4
gpt4 key购买 nike

我希望我在 UICollectionView 中的部分有一个带有图像的标题。

我遵循了这些步骤:

  • 在 Storyboard 中,为我的 UICollectionView
  • 分配一个标题作为附件
  • 给它一个标识符
  • 为其创建了 UICollectionReusableView 的子类
  • 将自定义类分配给 Storyboard 中的类。
  • 在标题附件中放置一个 ImageView
  • .h 文件中的自定义类中为 ImageView 创建了一个导出
  • viewDidLoad 中实现了以下内容:

[self.collectionView registerClass:[ScheduleHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;

if (kind == UICollectionElementKindSectionHeader)
{
ScheduleHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];

headerView.headerImageView.image = [UIImage imageNamed:@"blah.png"];

reusableview = headerView;
}

return reusableview;
}

我知道数据源和委托(delegate)方法正在工作,因为我可以看到所有单元格及其部分。但是,我没有标题。我在上面的方法中放置了一个断点,但它从未被调用。

我做错了什么?

最佳答案

看来您必须为 header 提供非零大小,否则 collectionView:viewForSupplementaryElementOfKind:atIndexPath 不会被调用。因此,要么像这样设置流布局的 headerReferenceSize 属性:

flowLayout.headerReferenceSize = CGSizeMake(self.collectionView.frame.size.width, 100.f);

swift 5+

flowLayout.headerReferenceSize = CGSize(CGSize(width: self.collectionView.frame.size.width, height: 100))

或者,如果您想按部分改变大小,请实现 collectionView:layout:referenceSizeForHeaderInSection

关于ios - 未调用 UICollectionReusableView 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18173191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com