未在UICollectionView中显示的标头甚至已在UICollectionViewFlowLayout中以编程方式注册

Moh*_*din 3 ios uicollectionview swift

即使我已经在UICollectionViewFlowLayout中以编程方式注册,我的标题似乎也没有出现.只是空白.

我的代码

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")
            }
        }
Run Code Online (Sandbox Code Playgroud)

Ant*_*hko 5

从Ryan Lee的答案中深入挖掘:

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

如果你只有一些部分的标题,但不是所有部分