首页 > 解决方案 > 无法将一种视图出列:UICollectionElementKindCell 与 Deferent ViewController 上的标识符

问题描述

**我已经在不同的视图控制器上设置了标识符,但它告诉我我必须为标识符注册一个 nib 或一个类,或者在情节提要中连接一个原型单元'**

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView == self.secondCollectionView {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath) as! CollectionViewCell
        cell.mainText.text = Imgname[indexPath.item]
        cell.mainImageView.image = mainImg[indexPath.item]
        return cell
    }
    else {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath) as! SecondCollectionViewCell
        cell.secondText.text = Imgname[indexPath.item]
        cell.secondImage.image = mainImg[indexPath.item]
        return cell
    }
}

标识符

标签: swiftidentifier

解决方案


var cell: UICollectionViewCell!

if .....{

     cell = 

} else {

     cell =

}

return cell

需要在函数中编写这种风格的代码


推荐阅读