Ios 子类化自定义CollectionView单元格不会';不显示父用户界面
我有一个从xib创建的自定义单元格 看起来是这样的: 我还有另外两个单元格,只在它的子类中创建 并且需要将不同的视图添加到Ios 子类化自定义CollectionView单元格不会';不显示父用户界面,ios,swift,uiview,uicollectionview,uikit,Ios,Swift,Uiview,Uicollectionview,Uikit,我有一个从xib创建的自定义单元格 看起来是这样的: 我还有另外两个单元格,只在它的子类中创建 并且需要将不同的视图添加到containerView 因此,在我的UIViewController中,我注册了所有单元格 @IBOutlet weak var collectionView: UICollectionView! { didSet { collectionView.dataSource = self collectionView.delegate
containerView
因此,在我的UIViewController
中,我注册了所有单元格
@IBOutlet weak var collectionView: UICollectionView! {
didSet {
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(cell1.self, forCellWithReuseIdentifier: "cell1")
collectionView.register(cell2.self, forCellWithReuseIdentifier: "cell2")
collectionView.register(UINib(nibName: "ParentCell", bundle: nil), forCellWithReuseIdentifier: "parentCell")
}
}
并将这些单元排在队列中
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.row == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! cell1
cell.containerView.backgroundColor = UIColor.yellow
return cell
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as! cell2
cell.containerView.backgroundColor = UIColor.red
return cell
}
但是应用程序崩溃了
致命错误:在展开可选值时意外发现nil
当我尝试使用containerView
有什么建议可以实施吗?谢谢
编辑: 我的问题是父单元格中的视图没有得到实例化。 我明白了 致命错误:在展开可选值时意外发现nil
意味着。我不明白为什么视图没有被实例化 飞机失事了
cell.containerView.backgroundColor
@Mr.Bista可能重复请再次阅读我的问题,它不一样。我知道,但错误是不言自明的,你能指出错误发生在哪一行吗?请参阅我的编辑。。。这没用。。。无论如何,谢谢“我不明白为什么视图没有被实例化。”,您可能输入了错误的单元格标识符名称。