5

I am using a custom UITableViewCell which already has a few UI elements on it that completely work. However, I just tried adding two labels to them and when I hook up the outlet and call cell.label.text = @"text";, the program crashes with the error:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<FeedTableViewCell 0x7aa53d00> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key likesLabel.'

I checked if there were any connections without elements on them and there were not. The app completely works when everything but new UI elements are added to the cell. It is just when I add new UI elements that the app crashes. What is the issue here?

Custom Cell Class .h

#import <UIKit/UIKit.h>

@interface FeedTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *profileImage;
@property (weak, nonatomic) IBOutlet UITextView *questionTextView;
@property (weak, nonatomic) IBOutlet UIButton *usernameButton;
@property (weak,nonatomic) IBOutlet UILabel * likesLabel;
@end

The likes label is the outlet with the issue

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{

    FeedTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[FeedTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }     
    cell.likesLabel.text = @"text";
    return cell;
}

The outlet connections for the cell:

enter image description here

random_0620
  • 1,636
  • 5
  • 23
  • 44
  • I noticed that `-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object` is not tableview's datasource method, what is it ? – KudoCC Aug 02 '14 at 02:33
  • That is a method from Parse which is the backend I use for my app. @KudoCC – random_0620 Aug 02 '14 at 02:47
  • I think you should load the cell from xib when it is needed, not initWithStyle.... – KudoCC Aug 02 '14 at 03:00
  • Create your cell using this, `NSArray *a = [[NSBundle mainBundle] loadNibNamed:@"YourCellXibName" owner:nil options:nil] ; cell = [a firstObject] ;`, don't forget set reuse identifier in your xib. – KudoCC Aug 02 '14 at 03:10
  • I put the cell in the storyboard not a separate xib so it uses initwithStyle. @KudoCC – random_0620 Aug 02 '14 at 03:13
  • So if you add another UI it will also crash - not just the likesLabel? – Robert J. Clegg Aug 02 '14 at 07:06
  • 3
    Yes it was actually anything added to the cell. I fixed the issue by cleaning the project. – random_0620 Aug 02 '14 at 12:48

4 Answers4

11

After spending way too much time on this, "Cleaning the Project" fixed this same mysterious issue for me. user1851782 mentions this as his final comment, but I thought I would highlight this. Using Xcode 6.

user3369429
  • 130
  • 1
  • 6
  • This also worked for me. I went to the extent of creating a new class and a new prototype cell with everything named the same except the class itself and it worked. It wasn't ideal but I couldn't see any error in the storyboard xml mappings, etc... So I moved on, it happened again and I knew something must be up with Xcode. I happened across your post. I don't know why I didn't think to clean before. A standard clean didn't do it for me, but a deep clean did. I up voted I hope that people find this. Too much time wasted on this issue! – David Greco Jan 26 '15 at 05:33
4

I fixed the same problem in about 30 minutes after I found this

I basically search on my code everything which had the key shown in the message and I found a label. Problem fixed after I canceled that label.

Mat
  • 6,236
  • 9
  • 42
  • 55
2

If you have any key that is also an existing method name (e.g. "description"), then iOS 8 will complain that you can't use that. You will get the KVC error and crash if you don't change that property name to something like "descLabel" or "descriptionLabel"

Caleb
  • 124,013
  • 19
  • 183
  • 272
kevinl
  • 4,194
  • 6
  • 37
  • 55
0

I just spent a morning trying to resolve this issue - and I eventually discovered the problem. When creating my own custom class for a UIControllerView I had the 'Test' folder selected and the files landed in that folder. To correct this I simply dragged the files to the App folder thinking nothing of it... BIG MISTAKE.

Once I deleted the files and recreated them in the correct folder everything worked fine...

Hope this helps someone...