首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未调用的UICollectionReusableView方法

未调用的UICollectionReusableView方法
EN

Stack Overflow用户
提问于 2013-08-11 14:54:00
回答 12查看 54.5K关注 0票数 94

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

我遵循了以下步骤:

  • 在故事板上,为我的UICollectionView分配了一个标题作为附件
  • 给了它一个标识符
  • 为它创建了一个UICollectionReusableView子类。
  • 将自定义类分配给故事板上的类。
  • 在头附件处放置一个ImageView
  • ImageView文件中的自定义类中为.h设置了一个出口
  • viewDidLoad上实现了以下功能

代码语言:javascript
复制
[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;
}

我知道数据源和委托方法是有效的,因为我可以看到所有的单元格及其部分。然而,我没有我的标题。我在上面的方法中输入了一个断点,而且它从未被调用过。

我做错什么了?

EN

回答 12

Stack Overflow用户

回答已采纳

发布于 2013-08-11 16:41:59

似乎你必须给你的标题一个非零的大小,否则collectionView:viewForSupplementaryElementOfKind:atIndexPath就不会被调用。因此,可以设置流布局的headerReferenceSize属性,如下所示:

代码语言:javascript
复制
flowLayout.headerReferenceSize = CGSizeMake(self.collectionView.frame.size.width, 100.f);

Swift 5+

代码语言:javascript
复制
flowLayout.headerReferenceSize = CGSize(CGSize(width: self.collectionView.frame.size.width, height: 100))

或者,如果希望按节更改大小,请实现collectionView:layout:referenceSizeForHeaderInSection

票数 224
EN

Stack Overflow用户

发布于 2014-06-18 22:19:03

你回答了这个问题,但用我的话来说,我更明白:

若要调用该方法,请添加以下方法:

代码语言:javascript
复制
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(60.0f, 30.0f);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(60.0f, 30.0f);
}

...and从那里出发。

票数 36
EN

Stack Overflow用户

发布于 2017-10-12 21:44:41

Swift 4 Xcode 9.1 Beta 2

加@objc

代码语言:javascript
复制
@objc func collectionView(_ collectionView: UICollectionView, layout  collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize{
    let size = CGSize(width: 400, height: 50)
    return size
}

学分:https://medium.com/@zonble/your-delegation-methods-might-not-be-called-in-swift-3-c6065ed7b4cd

票数 33
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18173191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档