【发布时间】:2017-11-22 10:46:34
【问题描述】:
我收到了经典消息“无法将同类视图出列:带有标识符歌曲的 UICollectionElementKindCell”。我不知道为什么。
*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将类型视图出列:带有标识符 song 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类或连接故事板中的原型单元'
我的单元格是一个自定义类,代码如下:
class HACollectionViewCell: UICollectionViewCell {
@IBOutlet var title: UILabel!
@IBOutlet var band_album: UILabel!
@IBOutlet var length: UILabel!
required init?(coder aDecoder: NSCoder) {
super.init(coder:aDecoder)
NSLog("I made a new cell!!")
//You Code here
}
}
插座已连接(即,当我将鼠标悬停在指示器上时,正确的东西会亮起)。单元格布局位于 Main.storyboard 中,作为 UICollectionView 对象的子级。
同样,单元格对象本身的“自定义类”中的类设置为 HACollectionViewCell。此外,UICollectionView 连接到我的 First View Controller 文件中的插座。
失败的函数:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "song", for: indexPath) as! HACollectionViewCell
let song = song_list[indexPath.row]
cell.title.text = song.title
cell.band_album.text = (song.artist ?? "unknown artist") + (song.albumTitle ?? "unknown album title")
cell.length.text = String(song.playbackDuration)
return cell
}
我已检查以确保我的重用标识符没有空格。正是“歌”。来自“Main.storyboard”:
<collectionReusableView key="sectionFooterView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="song" id="HEQ-RD-L8g" userLabel="Song Cell" customClass="HACollectionViewCell" customModule="Hamper" customModuleProvider="target">
顺便说一句,我在 macOS 10.12.5 上使用 Xcode 8.3.3 或 Xcode 9(测试版)。两个程序中的相同错误。目标是我运行 iOS 10.3.2 的 iPhone 5s。
【问题讨论】:
-
你在哪里设计了单元格?是用 Xib 还是 storyboard ?
-
啊,好问题。我会将答案添加到正文中。
标签: ios swift uicollectionview uicollectionviewcell