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

查看:322
本文介绍了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
    }

我们非常感谢您的帮助.

Any help is really appreciated.

推荐答案

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

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

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

似乎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天全站免登陆