2016-07-06 44 views
0

我創建了一個簡單的集合視圖和追加的虛擬數據(見下面的代碼)UICollectionViewDelegateFlowLayout使節頭消失

一切工作順利,直到我添加了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

我不能完全肯定,但我認爲你將不得不調用'setHeaderReferenceSize(your_size)'的' UICollectionViewFlowLayout'對象。 – avismara

回答

1

我設法做到這一點感謝@ avismara的建議。

在使用自定義佈局必須指定,像這樣在您的佈局尺寸:

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