未调用UICollectionReusableView方法

Ede*_* V. 69 ios uicollectionview

我希望我的部分UICollectionView有一个带图像的标题.

我已按照以下步骤操作:

  • 在故事板上,为我的分配了一个标题作为附件 UICollectionView
  • 给了它一个标识符
  • UICollectionReusableView为它创建了一个子类
  • 将自定义类分配给故事板上的类.
  • 放在ImageView标题附件
  • 为文件ImageView中的自定义类创建了一个出口.h
  • 在以下位置实施以下内容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;
}
Run Code Online (Sandbox Code Playgroud)

我知道数据源和委托方法正在工作,因为我可以看到所有单元格及其部分.但是,我没有我的标题.我在上面的方法中设置了一个断点,它永远不会被调用.

我究竟做错了什么?

rde*_*mar 178

看来你必须给你的标题一个非零大小或collectionView:viewForSupplementaryElementOfKind:atIndexPath不被调用.所以要么headerReferenceSize像这样设置流布局的属性:

flowLayout.headerReferenceSize = CGSizeMake(self.collectionView.frame.size.width, 100.f);
Run Code Online (Sandbox Code Playgroud)

或者,collectionView:layout:referenceSizeForHeaderInSection如果您想按部分改变大小,请执行.

  • 在集合视图中,我可以通过自动布局而不是定义 headerReference 大小来实现动态高度吗? (2认同)

小智 31

你回答了这个问题,但用语言我更了解:

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

- (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);
}
Run Code Online (Sandbox Code Playgroud)

......从那里开始


cwi*_*msz 22

Swift 4 Xcode 9.1 Beta 2

添加@objc

@objc func collectionView(_ collectionView: UICollectionView, layout  collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize{
    let size = CGSize(width: 400, height: 50)
    return size
}
Run Code Online (Sandbox Code Playgroud)

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

  • 如果你正在使用UICollectionViewFlowLayout,那么在没有@objc的情况下,符合UICollectionViewDelegateFlowLayout而不是UICollectionViewDelegate也可以解决这个问题. (6认同)
  • 仅将 @objc 添加到 Swift 4 中的函数对我有用。荒谬的。 (3认同)

小智 13

你在当前界面中添加了UICollectionViewDelegateFlowLayout吗?这对我有用.

@interface MyViewController () <UICollectionViewDelegateFlowLayout>

迅速:

class MyViewController:UICollectionViewDelegateFlowLayout {

  • 啊!所以这就是......`UICollectionViewDelegateFlowLayout` (2认同)

Xei*_*han 7

Swift 3.0(xcode 8.2.1)

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width:collectionView.frame.size.width, height:30.0)
}
Run Code Online (Sandbox Code Playgroud)


Kev*_*OUX 5

有快速版本:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    let size = CGSize(width: 400, height: 50)
    return size
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
    let size = CGSize(width: 400, height: 50)
    return size
}
Run Code Online (Sandbox Code Playgroud)

XCode(版本7.3.1)不会在自动完成中提出这些方法!

如果您只想要一个标题,请保留标题方法.