Search code examples
uitableviewuiimageviewuiimagenscfstring

NSCFConstantString size trying to set an Image


I'm making an UITableView with an UIIMage per cell. To set a different Image per cell I loaded all images in project and made an array with all images _flagsArray = [NSArray arrayWithObjects: @"flag_Italia.png", @"flag_USA", nil];.

In cellForRowAtIndexPath I wrote

UIImageView * flagImageView = (UIImageView *) [self.view viewWithTag:1]; //declaration of UIImageView in cell
UIImage * flagImage = (UIImage *)[_flagsArray objectAtIndex:indexPath.row]; //read UImage
[flagImageView setImage : flagImage]; //should assign UImage to UIImageView

Running it crash with log *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString size]: unrecognized selector sent to instance 0xcbdc', with signal SIGABRT on third row but I can't understand why, what's wrong?

Thank you!


Solution

  • Your array contains strings not images. Also, it looks like you're trying to get the image view from self.view when you should getting it from the cell. Do like this:

    UIImageView *flagImageView = (UIImageView *)[cell viewWithTag:1];
    NSString *imageName = _flagsArray[indexPath.row];
    UIImage *flagImage = [UIImage imageNamed:imageName];
    flagImageView.image = flagImage;
    

    Or more succinctly:

    UIImageView *flagImageView = (UIImageView *)[cell viewWithTag:1];
    flagImageView.image = [UIImage imageNamed:_flagsArray[indexPath.row]];