LoneWolfPR LoneWolfPR - 1 year ago 65
iOS Question

iOS: Custom Table Cell 'unrecognized selector sent to instance'

I have a tableView with a custom TableViewCell. I'm not using an .xib file to lay this out. The problem is when the table is supposed to load I get the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage nsli_superitem]: unrecognized selector sent to instance 0x91899b0'
I'm not sure what I'm doing wrong here. Here is the .m file for the cell.

#import "TCMExhibitListCell.h"

@implementation TCMExhibitListCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self setListImage:[[UIImage alloc] init]];
[self setTitleLabel:[[UILabel alloc] init]];

NSDictionary *names = @{@"listImage" : [self listImage]};

NSString *fmt = @"H:|-0-[listImage]";

NSArray *imgH = [NSLayoutConstraint constraintsWithVisualFormat:fmt
[[self contentView] addConstraints:imgH];
return self;

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
[super setSelected:selected animated:animated];

// Configure the view for the selected state


Here's where the cell gets loaded in the tableview controller

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
TCMExhibitListCell *c = [tableView dequeueReusableCellWithIdentifier:@"TCMExhibitListCell"];

if (!c) {
c = [[TCMExhibitListCell alloc] initWithStyle:UITableViewCellStyleDefault

TCMExhibitRemote *e = [[self exhibits] objectAtIndex:[indexPath row]];

NSString *imageFile = [NSString stringWithFormat:@"%@/%@.jpg",[[TCMExhibitFeedStore sharedStore] imagePathByType:@"listImage"],[e image]];

[c setListImage:[UIImage imageNamed:imageFile]];

return c;

Answer Source

If you do a little googling, you'll see that nsli_superitem is associated with auto-layout. Which points you towards [[self contentView] addConstraints:imgH];, which points you towards NSString *fmt = @"H:|-0-[listImage]";, and on to [self setListImage:[[UIImage alloc] init]];.

I.e. you can't set a layout based on an image, because it isn't a UI item.

You should probably be doing the layout based on an image view view somewhere...