简体   繁体   中英

Header in CollectionView not Displaying iOS Swift

I want to add a few buttons to the header of a Collection view. Enabled 'Section Header' from the storyboard. Then dragged a 'view' to that header. And put its background color to green

But I don't why it's not displaying. In the simulator as you see, only a blank space for the header. 在此处输入图片说明

Use this Method

Give name in CollectionHeaderCell

在此处输入图片说明 在此处输入图片说明

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

在此处输入图片说明

// make a cell for each cell index path

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
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM