views:

52

answers:

1

Hi guys I've spent this morning searching google for this, but I just can't get what I want how i want it.

I am creating a custom table view cell, with different icons down the side, in a sectioned table view. My problem is that I am having trouble reading these images from an array. I can do it like below, but can someone please help me do this from an array.

Working Long Form Code:

switch (indexPath.row) {
    case 0:
    imageView2.image = [UIImage imageNamed:@"ico-company.png"]; break;
    case 1:
    imageView2.image = [UIImage imageNamed:@"ico-value.png"];
    case 2:
    imageView2.image = [UIImage imageNamed:@"ico-date.png"]; break;
    case 3:
    imageView2.image = [UIImage imageNamed:@"ico-notes.png"];break;
    default:
    break;
    }

And i think i can get it to look something like:

Not Working Code how i want

   arryImages = [[NSMutableArray alloc] init];
    arryImages = [NSArray arrayWithObjects:
             [UIImage imageNamed: @"ico-company.png"],
                 [UIImage imageNamed: @"ico-value.png"],
                 [UIImage imageNamed: @"ico-date.png"],
                 [UIImage imageNamed: @"ico-notes.png"], nil];

    imageView2.image = [UIImage imageWithContentsOfFile:[arryImages objectAtIndex:[indexPath row]]];

And this is the error i get when i try my array code:

2010-01-18 13:20:47.314 SQL[60921:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIImage length]: unrecognized selector sent to instance 0x39135c0'

Regards

+4  A: 

Your last line should actually be:

imageView2.image = [arryImages objectAtIndex:[indexPath row]];

(in your code, you're passing an image object to imageWithContentsOfFile, but that method expects a filename, not an image object)

Philippe Leybaert
Perfect Answer!
norskben