【问题标题】:My super simple UICollectionView is not loading我的超级简单 UICollectionView 没有加载
【发布时间】:2021-01-30 01:13:01
【问题描述】:

我正在尝试创建一个简单的集合视图。我有我的自定义单元格“SectionCell”和我的自定义类“Section”,其中包含两个@IBOutlet 属性:titleLabel 和 imageView。这两个属性都连接到它们各自的故事板视图。

在故事板中,collectionView 场景已链接到继承自 UICollectionView 的 MenuVC.swift 文件。 Cell 视图链接到 SectionCell。我已将单元格的标识符设置为“部分”。

出于调试目的,我将 view.backgroundColor 设置为黑色,并将单元格的 contentView 背景颜色设置为蓝绿色。然而,当我运行模拟器时,两者都没有显示。我得到一个白色背景,出现的只是视图标题。关于修复的任何想法?

class MenuVC: UICollectionViewController {
    
    var sections = [Section]()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Begin Learning"
        
        view.backgroundColor = .black
    }

    
    // MARK:- CollectionView Methods
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 3
    }

    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Section", for: indexPath) as? SectionCell else {
            fatalError("Unable to dequeue SectionCell ")
        }
        
        let section = sections[indexPath.item]
        cell.titleLabel.text = section.title
        cell.imageView.image = UIImage(named: section.image)

        return cell
    }
}

class SectionCell: UICollectionViewCell {
    
    @IBOutlet var imageView: UIImageView!
    @IBOutlet var titleLabel: UILabel!
    
}

class Section: NSObject {
    
    var title: String
    var image: String
    
    init(title: String, image: String) {
        self.title = title
        self.image = image
    }
}

Simulator

我对在 SO 上发布问题也很陌生。如果您对如何更好地格式化问题有任何提示,我会全力以赴!

【问题讨论】:

  • 检查是否正在调用cellForItemAt - 在其中添加打印语句
  • 你为场景设置了自定义类吗?
  • cellForItem 没有被调用,是的,我确实将自定义类“MenuVC”设置为 UICollectionView 场景

标签: ios swift uikit uicollectionviewcell


【解决方案1】:

制作你的collectionview的出口并将它交给委托和数据源......

在您的视图控制器中制作这样的插座:

@IBOutlet weak var collectionView: UICollectionview!

然后把这段代码放到viewDidLoad()中

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.relaodData()
}

【讨论】:

  • 您好,感谢您的建议。但是,我认为它最适合我使用常规 UIViewController 的场景。相反,我选择了一个 UICollectionViewController,它已经带有一个 collectionView 属性并且方便地已经将委托和数据源分配给了自己。
【解决方案2】:

您是否实施了以下协议?

UICollectionViewDelegateFlowLayout

extension MenuVC: UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return .init(width: 300.0, height: 300.0) // and set size for each item inside this function.
    }
}

【讨论】:

  • 嗨,我对 UICollectionViewDelegateFlowLayout 很熟悉,但我认为它是可选的。在过去的项目中,我已经能够放弃它并且仍然可以渲染集合视图。
  • 当你将estimatedItemSize设置为自动(UICollectionViewFlowLayout)并使用每个Cell的自身大小时忽略
  • 在情节提要中,我已将estimateItemSize 设置为自动。不确定您所说的“并为每个单元格使用自身大小”是什么意思......但是,我现在收到错误消息:“'UICollectionView 必须使用非零布局参数进行初始化”。我还应该指出,在我的故事板视图层次结构中,我可以看到“集合视图流布局”旁边有一个黄色立方体。
【解决方案3】:

已解决

大声笑,作为开发人员,您确实需要鹰眼。这个:

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

应该是:

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

瞧!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2023-03-19
    • 2012-11-18
    • 1970-01-01
    相关资源
    最近更新 更多