2015-04-15 43 views
19

如何在swift中在集合視圖中創建頁眉和頁腳?如何使用swift在集合視圖中同時創建頁眉和頁腳

我試圖將一個頁眉和頁腳結合在一起,但它不斷崩潰,我無法找到迅速教程來理解它。

我不知道如何爲兩者返回補充視圖。

我把他們倆的故事板(+類標識符)

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 

} 

Error: UICollectionElementKindCell with identifier one - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

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

我希望有人可以幫助。

+0

請提供有關崩潰的更多信息 –

+0

帶標識符1的UICollectionElementKindCell - 必須註冊一個nib或類的標識符或連接故事板中的原型單元格 – marrioa

+0

標識符頁腳與UICollectionReusableView的頁腳單元格和標識符標題與UICollectionReusableView的標題單元格 – marrioa

回答

43

,您可根據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") 
    } 
} 

在上面的代碼中,我將identifier用於頁腳和頁眉,例如HeaderFooter例如,您可以按照需要進行操作。如果您想創建自定義頁眉或頁腳,那麼您需要爲每個頁面創建一個UICollectionReusableView的子類並根據需要對其進行自定義。

您可以在Interface Builder或通過以下方式代碼註冊自定義頁腳和頁眉類:

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

您是否有示例應用程序可供分享? – Learn2Code

4

更新了雨燕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") 
相關問題