如何在单元格内容视图中显示标签?

How to display label inside of a cells Content View?

我是 Swift 的初学者,我正在按照教程创建 UICollectionView。在教程中,当他创建单元格时,没有像 Xcode 中的“内容视图”小节。

在我的故事板上,我的标签位于单元格的 "Content View" 内。

我在单独的文件中有一个标签 (myLabel) 的出口 CollectionViewCell.swift

class CollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var myLabel: UILabel!

}

在我的页面视图控制器下我有这段代码

 //COLLECTION VIEW CODE STARTS

    let messageArray = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return messageArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell

        cell.myLabel.text = messageArray[indexPath.item]

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.item)
    }
    //COLLECTION VIEW CODE ENDS

一切正常,除了 myLabel 在 运行 应用程序中不可见,但我看到显示了所有 20 个单元格。如何配置此 "Content View" 以便显示我的标签?

可能是约束问题..检查你的约束