ios - UICollectionViewController 的单元格不显示

标签 ios swift uicollectionview

<分区>

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

这是我的 CollectionViewControllerClass 的代码

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)

该行告诉运行时从 Storyboard 中获取单元格。但是您确实想要从 Storyboard 中获取单元格,因为这是您更改单元格背景颜色的地方。

关于ios - UICollectionViewController 的单元格不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49098876/

相关文章:

ios - 我可以更新 iOS 应用程序以添加广告吗?

swift - swift 中 forEach 循环的进度条

ios - 为什么我的自定义 UITextField 在 Interface Builder 中与在设备上运行时看起来不同?

ios - 在 Watch InterfaceController.swift 中获取 MMWormhole 的未解析标识符

ios - 在 UICollectionViewCell 中获取图像

android - 我可以构建一个在所有手机上都兼容的 Web 应用程序吗?

ios - 为什么我看不到我的 TableViewRowActions 按钮(快速)?

ios - NSURLConnection 委托(delegate)方法未在泛型类中调用

ios - 如何在 Collection View 中的集合可重用 View 下添加元素

ios - 如何在 UICollectionView 中向下滚动时自动调用 PFQuery