UICollectionViewController页脚/标题未显示

时间:2018-06-15 09:54:49

标签: ios swift uicollectionview

我正在实施一个UICollectionViewController部分,我想在其中添加一个标题&页脚。 我已经阅读了RayWenderlich教程来实现逻辑,一切正常,除了我的标题&没有出现的页脚。

我检查过#34; Section Header" &安培; "章节页脚"在故事板检查器中,并为每个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")
    }
}

1 个答案:

答案 0 :(得分:1)

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

所以尝试替换函数定义

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

以下

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