作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在实现一个部分 UICollectionViewController
,我想在其中添加页眉和页脚。我已经阅读了 RayWenderlich 教程来实现逻辑并且一切正常,除了我的页眉和页脚没有显示。
我在 Storyboard检查器中检查了“Section Header”和“Section Footer”,并为每个 UICollectionReusableView
添加了带有“headerView”和“footerView”的标识符。
我实现了 viewForSupplementaryElementOfKind
方法(见下面的代码),但在 referenceSizeForFooterInSection
时没有触发(调试时没有触发断点) > 和 referenceSizeForHeaderInSection
是 .当应用程序启动时,我看不到页眉和页脚的背景颜色,但我可以看到一个应该是页眉和页脚的空间。
提前致谢!
这是我的 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/
我是一名优秀的程序员,十分优秀!