【问题标题】:UICollectionView with custom cell, fatal error: unexpectedly found nil while unwrapping an Optional value带有自定义单元格的 UICollectionView,致命错误:在展开可选值时意外发现 nil
【发布时间】:2017-09-21 17:14:02
【问题描述】:

我用UICollectionView来显示我的数据,我用了custom cell,在cellForItemAt有趣的应用程序在我想给标签赋值时崩溃了。

let reuseIdentifier = "brandCell"

ViewDidLoad------>

self.brandCollectionView!.register(BrandCercaCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        brandCollectionView.dataSource = self
        brandCollectionView.delegate = self


 // make a cell for each cell index path
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        // get a reference to our storyboard cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! BrandCercaCollectionViewCell
//App crashed here --->
        cell.brandNameLabel.backgroundColor = UIColor.blue
        return cell
    }

我附上了故事板的照片。

谢谢。

【问题讨论】:

  • 检查所有 IBOutlets 是否已连接...
  • 你不是要让一个还不存在的单元格出队吗? var cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! BrandCercaCollectionViewCell; if cell == nil { cell = BrandCercaCollectionViewCell(reuseIdentifier: reuseIdentifier)} 或类似的东西
  • 你在interface builder的字段中设置了可复用的标识符吗?
  • @GIJOW dequeueReusableCell(withReuseIdentifier:for:) 方法不返回可选项。
  • @AhmadF 它只是一个,你可以检查它的第一张图片

标签: ios swift uicollectionview custom-cell


【解决方案1】:

确保将情节提要中的单元格 “brandCell” 的标识符设置为集合视图单元格的标识符。

【讨论】:

  • 是的,我设置正确,我需要比它更多的解决方案,感谢您的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
  • 1970-01-01
  • 2016-01-26
相关资源
最近更新 更多