UICollectionViewController的单元格不显示[重复]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UICollectionViewController的单元格不显示[重复]相关的知识,希望对你有一定的参考价值。

这个问题在这里已有答案:

Xcode 9.2,Swift 4我在Main.storyboard中有一个CollectionViewController场景,我链接到CollectionVewController类。在这个场景中有一个单元格,其标识符是TextCell。我直接在Main.storyboard中更改了这个的背景颜色。

这是我的CollectionViewController类的代码

import UIKit

private let reuseIdentifier = "TextCell"

class CollectionViewController: UICollectionViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        self.collectionView?.backgroundColor = UIColor(red: 52.0 / 250.0, green: 63.0 / 250.0, blue: 77.0 / 250.0, alpha: 1)
    }

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)

        // Configure the cell
        return cell
    }
}

当我运行应用程序时,我看不到任何单元格,有人知道为什么吗?谢谢 !

答案

删除此行:

self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

该行告诉运行时没有从故事板中获取单元格。但您确实希望从故事板中获取单元格,因为这是您更改单元格背景颜色的位置。

以上是关于UICollectionViewController的单元格不显示[重复]的主要内容,如果未能解决你的问题,请参考以下文章

UISearchController 像 Instagram 探索选项卡

无法使用自动布局将简单的自行调整大小的集合视图单元格调整为标签大小