viewForSupplementaryElementOfKind 没有被调用 [英] viewForSupplementaryElementOfKind is not getting called

查看:21
本文介绍了viewForSupplementaryElementOfKind 没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经声明了如下的集合视图

i have declared collection view like below

lazy var collectionView:UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize  = UICollectionViewFlowLayout.automaticSize
        layout.minimumLineSpacing = 52
        layout.sectionInset = UIEdgeInsets(top: 25, left: 0, bottom: 30, right: 0)
        var collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(XXXCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        collectionView.register(XXXHeaderViewCell.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerIdentifier)
        collectionView.delegate = self
        return collectionView
    }()

并在委托下实现,控制权仅来自 referenceSizeForHeaderInSection 方法而不是 viewForSupplementaryElementOfKind

and implemented below delegates , control comes only to referenceSizeForHeaderInSection method but not to viewForSupplementaryElementOfKind

public func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath)
        return headerView
    }

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        let size = CGSize(width: self.collectionView.bounds.width, height: 200)
        return size
    }

非常感谢任何帮助.

推荐答案

我遇到了同样的问题,然后想出了一个解决方案.

I was facing the same issue and then figured out a solution.

尝试在 func collectionView(viewForSupplementaryElementOfKind:at:) 上方添加 @objc (collectionView:viewForSupplementaryElementOfKind:atIndexPath:).

try to add @objc (collectionView:viewForSupplementaryElementOfKind:atIndexPath:) just above your func collectionView(viewForSupplementaryElementOfKind:at:).

obj-C 和 Swift 似乎有不同的名称.看看最后一个参数,在 obj-C 上我们有 atIndexPath 而在 Swift 上我们只有 at.

It seems that the obj-C and Swift have different names. Look at the last param, on obj-C we have atIndexPath while on Swift we have just at.

这有帮助吗?

这篇关于viewForSupplementaryElementOfKind 没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆