LoneWolfPR
LoneWolfPR

Reputation: 4090

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
                                                            options:0
                                                            metrics:nil
                                                              views:names];
    [[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
}

@end

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
                               reuseIdentifier:@"TCMExhibitListCell"];
}

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

Upvotes: 3

Views: 5147

Answers (3)

JAL
JAL

Reputation: 42449

My issue was that I was trying to use a UIViewController in a constraint using Visual Format Language. Changing the constraint to use the UIViewController's view fixed the crash:

containerView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[playerVC]-0-|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["playerVC" : playerVC.view])) // note playerVC.view here, not playerVC

Upvotes: 1

Scheggia
Scheggia

Reputation: 1

I do think that you forget to set this.

self.listImage.translatesAutoresizingMaskIntoConstraints = NO;

Upvotes: 0

Wain
Wain

Reputation: 119031

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

Upvotes: 20

Related Questions