Ios UICollectionViewController页脚/页眉未显示
我正在实现一个单节Ios UICollectionViewController页脚/页眉未显示,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我正在实现一个单节UICollectionViewController,我想在其中添加页眉和页脚。 我已经阅读了RayWenderlich教程来实现逻辑,除了没有显示的页眉和页脚外,其他一切都很好 我在序列图像板检查器中选中了“节头”和“节尾”,并为每个UICollectionReusableView添加了带有“headerView”和“footerView”的标识符 我实现了supplementalelementofkind的视图(见下面的代码),但在引用了IzeForFooterInsect
UICollectionViewController
,我想在其中添加页眉和页脚。
我已经阅读了RayWenderlich教程来实现逻辑,除了没有显示的页眉和页脚外,其他一切都很好
我在序列图像板检查器中选中了“节头”和“节尾”,并为每个UICollectionReusableView
添加了带有“headerView”和“footerView”的标识符
我实现了supplementalelementofkind的视图(见下面的代码),但在引用了IzeForFooterInsection
和引用IzeForHeaderInstruction
时没有触发(调试时没有触发断点)。当应用程序启动时,我看不到页眉和页脚的背景色,但我可以看到一个应该是页眉和页脚的空间
提前谢谢你
以下是我的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")
}
}
我认为根据新语法,只有\u
(带空格的下划线)缺少
因此,请尝试替换函数定义
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
用下面的一个
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
我认为根据新语法,只有\u
(带空格的下划线)缺少
因此,请尝试替换函数定义
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
用下面的一个
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
尝试将func collectionView(collectionView:UICollectionView,viewforsupplmentalelementofkind-kind:String,atIndexPath indexPath:NSIndexPath)->UICollectionReusableView{
替换为func collectionView(\ucollectionview:UICollectionView,viewforsupplmentalelementofkind-kind:String,atIndexPath indexPath:nsindexath)->UICollectionReusableView{
我认为,根据新版本,缺少{/code>(下划线)syntax@iRiziya天哪,就是这样!如何因为下划线而浪费3个小时的时间。非常感谢:)回答我的问题,这样我就可以接受itTry替换func collectionView了(collectionView:UICollectionView,ViewForSupplmentalElementOfKind:String,atIndexPath indexPath:NSIndexPath)->UICollectionReusableView{
withfunc collectionView(collectionView:UICollectionView,ViewForSupplmentalElementOfKind:String,atIndexPath indexPath:NSIndexPath)->UICollectionReusableView{
我认为,\
(下划线)根据新的syntax@iRiziya天哪,就是这样!如何因为下划线而浪费我3个小时的生命。非常感谢:)回答我的问题,这样我就可以接受了