Error with custom UITableViewCell

I get this error:

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

Below is the code:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"ReservationCell"; ReservationCell *cell = (ReservationCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ReservationCell" owner:nil options:nil]; for (id currentObject in topLevelObjects){ if ([currentObject isKindOfClass:[UITableViewCell class]]){ cell = (ReservationCell *) currentObject; break; } } } //cell.origin.text = [[data objectAtIndex:indexPath.row] origin]; //cell.destination.text = [[data objectAtIndex:indexPath.row] destination]; //cell.time_range.text = [[data objectAtIndex:indexPath.row] time_range]; return cell; } 

Here is ReservationCell.h

 @interface ReservationCell : UITableViewCell { UILabel * origin; UILabel * destination; UILabel * time_range; } @property (nonatomic, retain) IBOutlet UILabel * origin; @property (nonatomic, retain) IBOutlet UILabel * destination; @property (nonatomic, retain) IBOutlet UILabel * time_range; @end 

This is how I connected it: enter image description here

+4
source share
6 answers
 NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ReservationCell" owner:nil options:nil]; 

Sets the owner of files to zero. Thus, you cannot connect any of your shortcuts to them. Instead, make sure the ReservationCell cell class and its outputs are connected to labels.

+4
source

For someone who has already reached this topic and could not find a solution like me, here is a quick solution to this problem:

In the interface builder, you linked your IBOutlets to the file owner when you need to link them to the cell view. That is why you get errors.

Good luck !!!

+10
source

This problem occurs when in the interface builder for the CustomCell nib element the user class File Owner is set to your CustomCell class.

  • A custom file owner class should always be set to a UITableViewCell.
  • The custom class of the table view representations object must be set to your CustomCell class.

You also need to initialize it with the name Nib of your table cell.

Example (NotificationCell is my own cell class): http://i42.tinypic.com/29pw0a8.png

+7
source

My exception was "NSUnknownKeyException", reason: "[setValue: forUndefinedKey:]: this class is not a key value compatible with the encoding for the key (and the name label that I had on CustomCell, I received this error only when I added something to CustomCell, e.g. label)

Problem with solution, my friend gave me excellent advice add MyCustomCell.m add to ProjectSettings → Assembly phases → add file MyCustomCell.m

+4
source

In my case, I added the interface for the cell to the header file, but did not have an implementation in the .m file. By simply adding an empty implementation (as shown below) for the class to the .m file, the problem is fixed.

 @implementation myTableViewCell @end 

Surprised this was not indicated as an answer, as it has bitten me several times in the past. Hope this helps someone!

+1
source

I had this problem when I duplicated the prototype cell in the storyboard and deleted one of the outputs in the cell. He left a link to the outlet, but is not associated with anything. Right-click on the prototype cell and find one of these yellow warning markers.

0
source

Source: https://habr.com/ru/post/1344154/


All Articles