2016-10-04 60 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.但它仍然在标题中显示空白 –

相关问题