2015-11-16 53 views
2

編程註冊我的頭好像沒有顯示出來,甚至我已經註冊程序中UICollectionViewFlowLayout。只是空白。頭沒有出現在UICollectionView起來,甚至已經在UICollectionViewFlowLayout

我的代碼

override func viewDidLoad() { 
    super.viewDidLoad() 
    UICollectionReusableViewlet layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
      layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 
      layout.itemSize = CGSize(width: 117, height: 117) 
      layout.minimumLineSpacing = 1 
      collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
      collectionView!.dataSource = self 
      collectionView!.delegate = self 
      collectionView!.registerClass(EventHeaderView.self, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: "MyHeaderView"); 
      collectionView!.registerClass(EventDetailCollectionViewCell.self, forCellWithReuseIdentifier:"MyCollectionViewCell") 
      collectionView!.backgroundColor = UIColor.blackColor() 
      self.view.addSubview(collectionView!) 
} 

    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { 
      switch kind { 
       //2 
      case UICollectionElementKindSectionHeader: 
       //3 
       let headerView = 
       collectionView.dequeueReusableSupplementaryViewOfKind(kind, 
        withReuseIdentifier: "HeaderView", 
        forIndexPath: indexPath) 
        as! EventHeaderView 
       headerView.lblName.text = "Anniversary" 
       headerView.lblDescription.text = "Venue" 
       return headerView 
      default: 
       //4 
       assert(false, "Unexpected element kind") 
      } 
     } 

請指點。謝謝。

回答

2
  1. 將數據源,委託從UICollectionView分配給UIViewController。
  2. 添加自定義UICollectionView類,它在故事板分配到UICollectionViewCell。還分配一個標識符。
  3. 設置節頭的督察。 (圖像)
  4. 一個頭單元將在集合視圖來生成。添加另一個UICollectionViewCell類並將該類分配給該單元。還設置一個標識符。

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

return 9 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("customCell", forIndexPath: indexPath) as! CustomCollectionViewCell 
    cell.counterLabel.text = "\(indexPath.item)" 
    return cell 
} 

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

    case UICollectionElementKindSectionHeader: 

     let headerView = 
     collectionView.dequeueReusableSupplementaryViewOfKind(kind,withReuseIdentifier: "headerCell", forIndexPath: indexPath) as! HeaderCollectionViewCell 
     return headerView 
    default: 
     assert(false, "Unexpected element kind") 
    } 
} 

enter image description here

這裏是link到GitHub上的項目。

+0

我已經做BRO包括設置頭標識符。 –

+0

確保您已設置高度爲情節提要/自定義UICollectionViewDelegateFlowLayout頭。它發生在我身上一次。我已經爲頭文件1定義了高度。 –

+0

已經通過檢查員設置了它。還是一樣。 –

1

確保您在佈局中加入這一行:

layout.headerReferenceSize = CGSize(width:0, height:50) 
4

挖掘從瑞安李的回答更深:

public func collectionView(_:UICollectionView, layout: UICollectionViewLayout, referenceSizeForHeaderInSection: Int) -> CGSize 
{ 
    if referenceSizeForHeaderInSection > 0 { 
     return CGSize.zero 
    } 
    return CGSize(width:0, height:50) 
} 

的情況下,你只有對一些章節標題,但不是所有的人