还有一个 "could not dequeue a view of kind: UICollectionElementKindCell with identifier song"

Yet another "could not dequeue a view of kind: UICollectionElementKindCell with identifier song"

我收到 classic 消息 "could not dequeue a view of kind: UICollectionElementKindCell with identifier song"。我说不出为什么。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier song - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

我的手机是自定义的 class,代码如下:

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 对象的子对象。

同样,单元格对象本身将 "Custom Class" 中的 Class 设置为 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
}

我已检查以确保我的重用标识符没有空格。正是"song"。来自 "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(测试版)。两个程序都出现同样的错误。目标是我的 iPhone 5s 运行 iOS 10.3.2.

<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">

为什么显示collectionReusableView?它应该是 collectionViewCell 类似的东西:

<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="song" id="HEQ-RD-L8g" userLabel="Song Cell" customClass="HACollectionViewCell" customModule="Hamper" customModuleProvider="target">

我想你用的是 Collection Reusable View 而不是 Collection View Cell