2015-12-15 123 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") 
    } 
} 
相关问题