Search code examples
iosswiftuicollectionviewuicollectionviewcell

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


I'm getting the classic message "could not dequeue a view of kind: UICollectionElementKindCell with identifier song". I can't tell why.

*** 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'

My cell is a custom class, here's the code:

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

The outlets are connected (i.e. the right things light up when I mouse over the indicators). The cell layout is in the Main.storyboard, as a child of the UICollectionView object.

Equally, the cell object itself has the Class in "Custom Class" set to HACollectionViewCell. Also, the UICollectionView is connected to an outlet in my First View Controller file.

The function where it fails:

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
}

I've checked to make sure my reuse identifier has no spaces. It's exactly "song". from "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">

BTW, I'm using Xcode 8.3.3 or Xcode 9 (beta) on macOS 10.12.5. Same error in either program. Target is my iPhone 5s running iOS 10.3.2.


Solution

  • In

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

    why is it showing collectionReusableView? It should be collectionViewCell something like:

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

    I think you have used Collection Reusable View instead of Collection View Cell.