我尝试使用两种类型的自定义集合视图单元格来实现以下行为,我们将其称为A和B:
我有一个带有2个选项,图像和音乐的分段控件。当我点击图像选项时,集合视图将通过使用自定义单元格A向我显示图像,当我点击音乐时,它将重新加载表格并通过使用自定义单元格B向我显示音乐。这通过使用相同的集合视图。
我的问题是我收到以下错误:
无法将类视图出列:UICollectionElementKindCell with 标识符PictureCell - 必须注册一个笔尖或类 标识符或连接故事板中的原型单元'
我目前的代码:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch mySegmentedControl.selectedSegmentIndex
{
case 0:
let nibPicture = UINib(nibName: "PictureCell", bundle: nil)
myCollectionView.register(nibPicture, forCellWithReuseIdentifier: "Cell")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for:indexPath) as! PictureCollectionViewCell
let fruits: [MyItem] = dataSource.itemsInGroup(indexPath.section)
let fruit = fruits[indexPath.row]
let name = fruit.name!
cell.fileImage.image = UIImage(named: name.lowercased())
return cell
case 1:
let nibMusic = UINib(nibName: "MusicCell", bundle: nil)
myCollectionView.register(nibMusic, forCellWithReuseIdentifier: "Cell")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for:indexPath) as! MusicCollectionViewCell
let fruits: [MyItem] = dataSource.itemsInGroup(indexPath.section)
let fruit = fruits[indexPath.row]
let name = fruit.name!
cell.labelAuthor.text = name
cell.labelMediaName.text = name
return cell
default:
let nibPicture = UINib(nibName: "PictureCell", bundle: nil)
myCollectionView.register(nibPicture, forCellWithReuseIdentifier: "Cell")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for:indexPath) as! PictureCollectionViewCell
let fruits: [MyItem] = dataSource.itemsInGroup(indexPath.section)
let fruit = fruits[indexPath.row]
let name = fruit.name!
cell.fileImage.image = UIImage(named: name.lowercased())
return cell
}
}
图像的初始加载效果很好但是当我更改为音乐时它会崩溃。 以下是演示此问题的演示:Demo Link