gpt4 book ai didi

ios - UICollectionViewController 页脚/页眉未显示

转载 作者:行者123 更新时间:2023-11-28 14:35:59 25 4
gpt4 key购买 nike

我正在实现一个部分 UICollectionViewController,我想在其中添加页眉和页脚。我已经阅读了 RayWenderlich 教程来实现逻辑并且一切正常,除了我的页眉和页脚没有显示。

我在 Storyboard检查器中检查了“Section Header”和“Section Footer”,并为每个 UICollectionReusableView 添加了带有“headerView”和“footerView”的标识符。

我实现了 viewForSupplementaryElementOfKind 方法(见下面的代码),但在 referenceSizeForFooterInSection 时没有触发(调试时没有触发断点) >referenceSizeForHeaderInSection 是 .当应用程序启动时,我看不到页眉和页脚的背景颜色,但我可以看到一个应该是页眉和页脚的空间。

提前致谢!

enter image description here

enter image description here

这是我的 ViewController 的摘录:

class VoteNextCityViewController : UICollectionViewController, UICollectionViewDelegateFlowLayout {
fileprivate let sectionInsets = UIEdgeInsets(top: 50.0, left: 20.0, bottom: 50.0, right: 20.0)
fileprivate let itemsPerRow: CGFloat = 2

private let cellID = "cellID"
var pendingCityArray: [City] = []

override func viewDidLoad() {
super.viewDidLoad()
fetchVotedCities()
}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! VoteNextCityCell
let city = pendingCityArray[indexPath.row] //cannot not be nil, else numberOfItemsInSection would return 0
cell.setupView(city: city)
return cell
}

override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pendingCityArray.count
}



func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
//2
let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
let availableWidth = view.frame.width - paddingSpace
let widthPerItem = availableWidth / itemsPerRow

return CGSize(width: widthPerItem, height: widthPerItem+50)
}



func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return sectionInsets
}

func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return sectionInsets.left
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {

return CGSize(width: (self.collectionView?.frame.size.width)!, height: 50)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: (self.collectionView?.frame.size.width)!, height: 50)

}
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionFooter :
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "footerView", for: indexPath as IndexPath)
view.backgroundColor = UIColor.blue

return view
case UICollectionElementKindSectionHeader:
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerView", for: indexPath as IndexPath)
view.backgroundColor = UIColor.red

return view

default:
assert(false, "Unexpected element kind")
}
}

最佳答案

我认为根据新语法,只有 _(带空格的下划线)缺失。

所以尝试替换函数定义

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

与下面的

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

关于ios - UICollectionViewController 页脚/页眉未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50873177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com