UICollectionViewDelegateFlowLayout使节标题消失

时间:2016-07-06 14:10:50

标签: ios swift uicollectionview

我创建了一个简单的集合视图并添加了虚拟数据(参见下面的代码)

在我添加CollectionViewDelegateFlowLayout之前,一切运行良好,它使我的节标题消失。知道为什么会发生这种情况以及我如何同时拥有我的节标题和自定义布局?

代码:

import UIKit

private let reuseIdentifier = "Cell"

class HomeCollectionViewController: UICollectionViewController {

var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]

override func viewDidLoad() {
    super.viewDidLoad()

    // Setup a layout
    setupCollectionViewLayout(minimumInteritemSpacing: 0, minimumLineSpacing: 0)
}


// MARK: UICollectionViewDataSource

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 2
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return items.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell

    // Configure the cell
    cell.myLabel.text = items[indexPath.item]

    return cell
}

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as! MyCollectionReusableView

    headerView.sectionLabel.text = "Toto"

    return headerView

}

func setupCollectionViewLayout(minimumInteritemSpacing minimumInteritemSpacing: CGFloat, minimumLineSpacing: CGFloat) {
    let layout = UICollectionViewFlowLayout()
    layout.minimumInteritemSpacing = minimumInteritemSpacing
    layout.minimumLineSpacing = minimumLineSpacing
    collectionView?.collectionViewLayout = layout
}

}

// ----------------------------------------------------------------------------------
// MARK: - UICollectionViewDelegateFlowLayout
// ----------------------------------------------------------------------------------

extension HomeCollectionViewController: UICollectionViewDelegateFlowLayout {

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    switch UIDevice.currentDevice().userInterfaceIdiom {
    case .Phone:
        return CGSize(width: self.view.frame.width, height: 100.0)
    case .Pad:
        return CGSize(width: self.view.frame.width/2.0, height: 100.0)
    default:
        return CGSize(width: self.view.frame.width, height: 100.0)
    }


}
}

1 个答案:

答案 0 :(得分:1)

由于@ avismara的建议,我设法做到了。

使用自定义布局时,您必须在布局上指定尺寸,如下所示:

// Supposing you have created a layout, add:
layout.headerReferenceSize = CGSizeMake(HEADER_WIDTH, HEADER_HEIGHT)