0

I am using a NIB file to build my custom collection view cell. The File Owner of the NIB contains this code:

import UIKit

class CustomCollectionViewCell: UICollectionViewCell {
    
    @IBOutlet weak var imageView: UIImageView!
    
    @IBOutlet weak var statusTextView: UITextView!
    
    @IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
    
    @IBOutlet weak var titleTextView: UITextView!
    
    @IBOutlet weak var infoTextView: UITextView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
}

All of the IBOutlets are connected to the corresponding view in the NIB. I have also registered my NIB using this in the view controller's viewWillAppear method:

collectionView.register(UINib(nibName: "CustomCollectionViewCell", bundle: .main), forCellWithReuseIdentifier: "image")

and I also set the reuse identifier to "image" in StoryBoard and set the class of the cell to my CustomCollectionViewCell. Yet, I am getting this crash error:

Thread 1: "[<NSObject 0x6000012f0b50> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key activityIndicatorView."

I appreciate your help.

David Hsieh
  • 9
  • 1
  • 2
  • 1
    You have an instance of `NSObject`, not `CustomCollectionViewCell` so you haven't set the custom class correctly in your nib – Paulw11 Jan 09 '22 at 06:31
  • @Paulw11 But in the identity inspector of my NIB file, I set the class to CustomCollectionViewCell. I'm still confused. – David Hsieh Jan 09 '22 at 06:38
  • 1
    Check here. https://stackoverflow.com/questions/13793162/setvalueforundefinedkey-this-class-is-not-key-value-coding-compliant-for-the-k – Imran0001 Jan 09 '22 at 06:47
  • The exception says you have an NSObject... – Paulw11 Jan 09 '22 at 06:59
  • @Paulw11 So how do I change it to the correct class? – David Hsieh Jan 09 '22 at 07:26
  • You have changed iboutlet name maybe, or deleted some outlet, but IB still have connected to it. – Anton Jan 09 '22 at 09:59
  • Select activity indicator on your xib a check the connections inspector. Maybe you have 2 in there. – Gustavo Conde Jan 09 '22 at 10:30
  • @DavidHsieh - Your question states *"The File Owner of the NIB ..."* -- So, it sounds like you've assigned the `CustomCollectionViewCell` Class to the `File's Owner` when it needs to be assigned to the `Cell`. So, assign the `Class` to the *Cell*: https://i.stack.imgur.com/ZUYsU.png and leave `File's Owner` blank (the default NSObject): https://i.stack.imgur.com/mdEr7.png **Note:** if that was the case, you'll need to re-connect the IBOutlets after making this change. – DonMag Jan 09 '22 at 14:13
  • For anyone who has the same problem, I finally figured it out. I wrongly hooked up the IBOutlets to File's Owner instead of the nib object. Once I switched, everything works!!! FINALLY!!!!!!!!!!!!!!! So, none of the answers posted here or in the "Duplicate" question actually solved my problem. – David Hsieh Jan 10 '22 at 00:06

1 Answers1

0

You need to set the type of view in interface builder: Shows Interface Builder with custom class set.

Look how the name of the custom class is in the right pane.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72