2015-12-15 104 views
4

我在控制器類中添加了委託函數,但它在運行時不會調用。如何在Swift中添加UICollectionView的頁眉和頁腳視圖

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

http://www.appcoda.com/supplementary-view-uicollectionview-flow-layout/ – vaibby

+0

你已經註冊爲補充視圖類? – rmaddy

+2

可能重複http://stackoverflow.com/questions/29655652/how-to-make-both-header-and-footer-in-collection-view-with-swift – Khuong

回答

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

    switch kind { 

    case UICollectionElementKindSectionHeader: 

     let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as! UICollectionReusableView 

     headerView.backgroundColor = UIColor.blueColor(); 
     return headerView 

    case UICollectionElementKindSectionFooter: 
     let footerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Footer", forIndexPath: indexPath) as! UICollectionReusableView 

     footerView.backgroundColor = UIColor.greenColor(); 
     return footerView 

    default: 

     assert(false, "Unexpected element kind") 
    } 
} 
+0

這個委託沒有在運行時調用 –

+1

我打電話給:(Swift 3) 'collectionView ?.register(HeaderCollectionViewCell.self,forSupplementaryViewOfKind:UICollectionElementKindSectionHeader,withReuseIdentifier:「HeaderCell」) 我仍然無法調用委託! – rn3sto

2

委託方法不叫,因爲你需要註冊爲補充視圖類;

yourCollectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "collectionHeaderID") 

yourCollectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "collectionFooterID") 
2

如果使用自定義UICollectionViewFlowLayout

集headerReferenceSize

let flowLayout = UICollectionViewFlowLayout() 
    flowLayout.scrollDirection = .vertical 
    flowLayout.minimumLineSpacing = 0 
    flowLayout.minimumInteritemSpacing = 0 
    flowLayout.sectionHeadersPinToVisibleBounds = true 
    flowLayout.headerReferenceSize = CGSize(width:  self.collectionView.frame.size.width, height: 50) 
0

我按照這個教程Appcoda tutorial和更新代碼爲SWIFT 4.0

註冊類:

self.collectionView.register(HeadView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeadView") 

代表:

// For header size (required) 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
    return CGSize(width:collectionView.frame.size.width, height:50.0) 
} 


func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 

    switch kind { 

    case UICollectionElementKindSectionHeader: 

     let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeadView", for: indexPath) 

     headerView.backgroundColor = UIColor.blue; 
     return headerView 

    default: 

     fatalError("Unexpected element kind") 
    } 
} 
相關問題