UICollection视图自定义标题未显示Swift 4

时间:2018-06-11 19:58:56

标签: ios swift uicollectionview

请帮助,我一直在试图弄清楚为什么我的代码不起作用。我想要做的是为我的集合视图设置一个自定义标题,但似乎我注册的类似乎没有触发。 它甚至没有显示红色框。它只显示青色空白背景

我做错了什么?

snapshot

以下是我的代码:

class VideoDetailController : UICollectionViewController, UICollectionViewDelegateFlowLayout {
    private let headerId = "headerId"
    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.backgroundColor = UIColor.cyan

        collectionView?.register(VideoDetailHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerId)

        navigationItem.title = "Hello!"
    }

    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionViewCell {

        let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerId, for: indexPath) as! VideoDetailHeader
        return header
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.frame.width,height:170)
    }
}


class VideoDetailHeader: BaseCell {    
    override func setupViews() {
        print("Setting up...")
        super.setupViews()
        backgroundColor = UIColor.red
    }
}

BaseCell是UICollectionViewCell

1 个答案:

答案 0 :(得分:0)

以上代码似乎有效: 唯一的问题是当我调用这个视图控制器时,我使用了: let layout = UICollectionViewLayout() 代替 : let layout = UICollectionViewFlowLayout()

相关问题