如何使用swift在集合视图中创build页眉和页脚

如何使swift中的集合视图中的页眉和页脚?

我试图将一个页眉和一个页脚结合在一起,但它不断崩溃,我找不到快速教程来理解它。

我不知道如何为两者返回补充观点。

我把它们都放在故事板上(class +标识符)

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { //#warning Incomplete method implementation -- Return the number of sections return 2 } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { //#warning Incomplete method implementation -- Return the number of items in the section return 10 } override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { var header: headerCell! var footer: footerCell! if kind == UICollectionElementKindSectionHeader { header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath) as? headerCell } return header } 

错误:标识符为1的UICollectionElementKindCell – 必须为标识符注册一个笔尖或类,或者在故事板中连接一个原型单元格“

 override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> profileCC { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("one", forIndexPath: indexPath) as! profileCC // Configure the cell return cell } 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! headerCell headerView.backgroundColor = UIColor.blueColor(); return headerView case UICollectionElementKindSectionFooter: let footerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "footer", forIndexPath: indexPath) as! footerCell footerView.backgroundColor = UIColor.greenColor(); return footerView default: assert(false, "Unexpected element kind") } } 

我希望有人会帮助。

您可以使UICollectionViewController处理UICollectionView并在Interface Builder中激活页脚页眉部分,然后您可以使用以下方法在您的UICollectionView预览添加的两个部分:

 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") } } 

在上面的代码中,我将HeaderHeaderidentifier作为HeaderFooter ,例如,您可以按照自己的意愿进行操作。 如果你想创build一个自定义的页眉或页脚,那么你需要为每个创build一个UICollectionReusableView的子类,并根据需要进行自定义。

您可以通过以下方式在Interface Builder或代码中注册自定义页脚和标题类:

 registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "myFooterView") 

更新了Swift 3.0

 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { switch kind { case UICollectionElementKindSectionHeader: let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath) headerView.backgroundColor = UIColor.blue return headerView case UICollectionElementKindSectionFooter: let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Footer", for: indexPath) footerView.backgroundColor = UIColor.green return footerView default: assert(false, "Unexpected element kind") } } 

和:

 self.collectionView.register(MyClass.self, forCellWithReuseIdentifier: "MyClass")