Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header not showing up in UICollectionView even already registered programmatically in UICollectionViewFlowLayout

My header seems not showing up even I already registered programmatically in UICollectionViewFlowLayout. Just blank.

My code

override func viewDidLoad() {
    super.viewDidLoad()
    UICollectionReusableViewlet layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
            layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            layout.itemSize = CGSize(width: 117, height: 117)
            layout.minimumLineSpacing = 1
            collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
            collectionView!.dataSource = self
            collectionView!.delegate = self
            collectionView!.registerClass(EventHeaderView.self, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: "MyHeaderView");
            collectionView!.registerClass(EventDetailCollectionViewCell.self, forCellWithReuseIdentifier:"MyCollectionViewCell")
            collectionView!.backgroundColor = UIColor.blackColor()
            self.view.addSubview(collectionView!)
}

    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
            switch kind {
                //2
            case UICollectionElementKindSectionHeader:
                //3
                let headerView =
                collectionView.dequeueReusableSupplementaryViewOfKind(kind,
                    withReuseIdentifier: "HeaderView",
                    forIndexPath: indexPath)
                    as! EventHeaderView
                headerView.lblName.text = "Anniversary"
                headerView.lblDescription.text = "Venue"
                return headerView
            default:
                //4
                assert(false, "Unexpected element kind")
            }
        }
like image 407
Nurdin Avatar asked Dec 18 '22 22:12

Nurdin


2 Answers

Digging deeper from Ryan Lee's answer:

public func collectionView(_:UICollectionView, layout: UICollectionViewLayout, referenceSizeForHeaderInSection: Int) -> CGSize
{
    if referenceSizeForHeaderInSection > 0 {
        return CGSize.zero
    }
    return CGSize(width:0, height:50)
}

in case you have headers only for some sections, but not all of them

like image 167
Anton Tropashko Avatar answered Apr 29 '23 07:04

Anton Tropashko


  1. Assign datasource, delegate from UICollectionView to UIViewController.
  2. Add custom UICollectionView class and assign it to UICollectionViewCell in storyboard. Also assign an identifier.
  3. Set section header in inspector. (Image)
  4. A header cell will be generated in collection view. Add another UICollectionViewCell class and assign this class to that cell. Also set an identifier.

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{

    return 9
}

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

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("customCell", forIndexPath: indexPath) as! CustomCollectionViewCell
    cell.counterLabel.text = "\(indexPath.item)"
    return cell
}

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    switch kind {

    case UICollectionElementKindSectionHeader:

        let headerView =
        collectionView.dequeueReusableSupplementaryViewOfKind(kind,withReuseIdentifier: "headerCell", forIndexPath: indexPath) as! HeaderCollectionViewCell
        return headerView
    default:
        assert(false, "Unexpected element kind")
    }
}

enter image description here

Here is link to github project.

like image 27
Saqib Omer Avatar answered Apr 29 '23 06:04

Saqib Omer