ios 未调用 UICollectionReusableView 方法

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

UICollectionReusableView method not being called

iosuicollectionview

提问by Eden V.

I want my sections in the UICollectionViewto have a header with an image.

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

I have followed these steps:

我已按照以下步骤操作:

  • at the storyboard, assigned a header as an accessory for my UICollectionView
  • gave it an identifier
  • created a subclass of UICollectionReusableViewfor it
  • assigned the custom class to the class at the storyboard.
  • put an ImageViewat the header accessory
  • made an outlet for the ImageViewat the custom class in the .hfile
  • Implemented the following at viewDidLoad:
  • 在故事板中,为我分配了一个标题作为附件 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;
}

I know that the datasource and delegate methods are working because I can see all the cells and its sections. However, I don't have my headers. I put in a breakpoint at the method above and it's never being called.

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

What am i doing wrong?

我究竟做错了什么?

回答by rdelmar

It seems that you have to give your header a non-zero size or collectionView:viewForSupplementaryElementOfKind:atIndexPathisn't called. So either set the headerReferenceSizeproperty of the flow layout like so:

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

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

or, implement collectionView:layout:referenceSizeForHeaderInSectionif you want to vary the size by section.

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

回答by Pear Johan

You answered the question, but in words I understand better:

你回答了这个问题,但我更理解的话:

To make the method being called, add these methods:

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

- (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 go from there.

...然后从那里走。

回答by cwilliamsz

Swift 4 Xcode 9.1 Beta 2

斯威夫特 4 Xcode 9.1 测试版 2

Add @objc

添加@objc

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

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

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

回答by CheshireKat

Did you add UICollectionViewDelegateFlowLayout in current interface? This worked for me.

您是否在当前界面中添加了 UICollectionViewDelegateFlowLayout?这对我有用。

@interface MyViewController () <UICollectionViewDelegateFlowLayout>

@interface MyViewController () <UICollectionViewDelegateFlowLayout>

Swift:

迅速:

class MyViewController: UICollectionViewDelegateFlowLayout {

class MyViewController: UICollectionViewDelegateFlowLayout {

回答by Xeieshan

Swift 3.0 (xcode 8.2.1)

斯威夫特 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)
}

回答by Kevin ABRIOUX

There is the swift version :

有快速版本:

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
}

XCode (version 7.3.1 ) does not propose these methods in autocomplete !

XCode(7.3.1 版)在自动完成中没有提出这些方法!

If you just want a header, just keep the header method.

如果你只想要一个标题,只需保留标题方法。

回答by Daniel Jones

My problem was that in swift 3, the name of the viewForSupplementaryElementOfKind function has changed slightly from any posts that were on stack overflow. Hence, it was never getting called. Make sure that, if you're in swift 3, you're delegate function matches:

我的问题是,在 swift 3 中,viewForSupplementaryElementOfKind 函数的名称与堆栈溢出的任何帖子略有不同。因此,它从未被调用。确保,如果你在 swift 3,你是委托函数匹配:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {}

回答by Twinkle

I have the same issue. I have added referenceSizeForFooterInSectionbut then also viewForSupplementaryElementOfKindnot getting called. I have added this code in viewDidLoad()and it worked for me:

我有同样的问题。我添加了referenceSizeForFooterInSection但也viewForSupplementaryElementOfKind没有被调用。我已经添加了这段代码viewDidLoad(),它对我有用:

if let flowLayout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    flowLayout.sectionFootersPinToVisibleBounds = true   
}

回答by trishcode

For me, in Swift 4.2, func collectionView(collectionView:kind:indexPath:) -> UICollectionReusableView was never being called. This was for a collection view in a UIViewController. I noticed the existing collection view functions were qualified with @objc, and that the UICollectionView had not adopted the UICollectionViewDataSource and UICollectionViewDelegate protocols. As soon as I adopted the protocols, I got errors that the collection view functions did not match the protocol. I corrected the function syntax, removed the qualifiers, and the section headers started working.

对我来说,在 Swift 4.2 中,从未调用 func collectionView(collectionView:kind:indexPath:) -> UICollectionReusableView。这是用于 UIViewController 中的集合视图。我注意到现有的集合视图函数被 @objc 限定,并且 UICollectionView 没有采用 UICollectionViewDataSource 和 UICollectionViewDelegate 协议。一旦我采用了协议,我就收到了集合视图函数与协议不匹配的错误。我更正了函数语法,删除了限定符,并且节标题开始工作。

回答by Bibin Jaimon

    @objc (collectionView:viewForSupplementaryElementOfKind:atIndexPath:)
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { }

The above code worked for me

上面的代码对我有用