CollectionView中的标题不显示iOS Swift

时间:2016-10-04 11:53:05

标签: ios swift swift2 uicollectionview swift3

我想在Collection视图的标题中添加几个按钮。从故事板启用“Section Header”。然后将“视图”拖动到该标题。并将其背景颜色设为绿色

但我不知道为什么它没有显示。在您看到的模拟器中,只有标题的空白区域。 enter image description here

1 个答案:

答案 0 :(得分: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
 }