ios - viewForSupplementaryElementOfKind 没有被调用
问题描述
我已经声明了如下的集合视图
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
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
}
非常感谢任何帮助。
解决方案
我遇到了同样的问题,然后想出了一个解决方案。
尝试在@objc (collectionView:viewForSupplementaryElementOfKind:atIndexPath:)
您的func collectionView(viewForSupplementaryElementOfKind:at:)
.
似乎 obj-C 和 Swift 有不同的名称。看看最后一个参数,在 obj-C 上我们有atIndexPath
,而在 Swift 上我们只有at
.
那有帮助吗?
推荐阅读
- firebase - 尝试使用 Firebase 电话号码身份验证注册时阻止浏览器弹出
- groovy - SoapUI 开源项目测试用例无限循环运行
- java - NoClassDefFoundError: org/w3c/dom/ls/DocumentLS 当 log4j 在另一个模块中使用时
- javascript - 即使重新加载后变量值仍然存在
- packet - 以 1 模 256 递增
- case - Teradata 从多行中获取变量的值
- java - Maven exec 目标不提供正确的输出
- python - Python - 在函数中输入未指定数量的参数
- flutter - 按下按钮时更新“列”的“文本”小部件
- python - 如何设置 ChromeOptions 以设置在 Python 中使用 Selenium 下载网页的默认目录和文件名?