2016-10-04 102 views
2

我想向集合視圖的標題添加幾個按鈕。從故事板中啓用'Section Header'。然後將「視圖」拖到該標題。並把它的背景顏色爲綠色CollectionView中的標題不顯示iOS Swift

但我不明白爲什麼它不顯示。如您所見,在模擬器中,只有標題的空白區域。 enter image description here

+0

你有沒有宣佈'可選FUNC的CollectionView(_的CollectionView:UICollectionView,viewForSupplementaryElementOfKind類型:字符串,在indexPath:IndexPath) - > UICollectionReusableView'在索裏的代碼? – ezcoding

+0

檢查我的答案。 –

+0

@ezcoding嘗試添加,但給我錯誤 –

回答

3

使用此方法

CollectionHeaderCell

enter image description here enter image description here

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 as IndexPath) 

     headerView.backgroundColor = UIColor.blue; 
     return headerView 

    case UICollectionElementKindSectionFooter: 
     let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Footer", for: indexPath as IndexPath) 

     footerView.backgroundColor = UIColor.green; 
     return footerView 

    default: 

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

enter image description here

給名//使一個細胞每個單元索引路徑

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

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("InviteFriendCell", forIndexPath: indexPath) 

     let profileimage: UIImageView = (cell.viewWithTag(5) as! UIImageView) 

     profileimage.image = UIImage(named:arrayofdata[indexPath.row]["image"]!) 
     return cell 
} 
+0

試過了,不工作 –

+0

它完全正常工作我有上傳屏幕截圖檢查它。並比較你的代碼和我的代碼。 –

+1

我試過了。將所有語法轉換爲swift 3.但它仍然在標題中顯示空白 –

相關問題