Graeme
Graeme

Reputation: 1327

Adding a dynamic image to UITable View Cell

I have a table in which I want a dynamic image to load in at the left-hand side. The table needs to use an IF statement to select the appropriate image from the "Resources" folder, and needs to be based upon [dog types].

The [dog types] is extracted from an RSS feed, and so the image in the table cell needs to match the each cell's [dog types] tag.

Update: See code below for what I'm looking to do (only below it's for earthquakes, for me its pictures of dogs).

I suspect I need to use - (UIImage *)imageForTypes:(NSString *)types { to do such a thing.

Thanks.

Updated code: This is for Apple's Earthquake sample but does exactly what I need to do. It matches images to the severity (2.0, 3.0, 4.0, 5.0 etc.) of every earthquake to the magnitude.

- (UIImage *)imageForMagnitude:(CGFloat)magnitude { 
    if (magnitude >= 5.0) {
        return [UIImage imageNamed:@"5.0.png"];
    }
    if (magnitude >= 4.0) {
        return [UIImage imageNamed:@"4.0.png"];
    }
    if (magnitude >= 3.0) {
        return [UIImage imageNamed:@"3.0.png"];
    }
    if (magnitude >= 2.0) {
        return [UIImage imageNamed:@"2.0.png"];
    }
    return nil;
}

and under - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

magnitudeImage.image = [self imageForMagnitude:earthquake.magnitude];

Upvotes: 2

Views: 4100

Answers (3)

Williham Totland
Williham Totland

Reputation: 29009

The image is set in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

You might want to have that method anyway, for brevity, but you just set the image as you would set a static one.

Edit: That is to say; in that particular method:

cell.imageView.image = [UIImage imageNamed:@"Dachshund"]; // Dachshund.jpg exists in the resources directory, of course.

Re-edit: To be even more precise:

// Assuming that a) [dog types] is "<name of dog type>" and;
// b) an image named "<name of dog type>.file_extension" exists in the resources of the project:
cell.image = [UIImage imageNamed:[[dog types] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
// No mapping of the string to image name is necessary if you know the
// set of strings that will be used for the feed.

Upvotes: 1

Efrain
Efrain

Reputation: 3364

I never used this, but I think you can use the imageView property of a table cell (UITableViewCell) to add images:

@property(nonatomic, readonly, retain) UIImageView *imageView

It is a pointer to a UIImageView, which is created (alloc) by the cell itself (that's why its readonly). Generally, we can load an image into a UIImageView like this with the initWithImage method:

UIImageView *myImgView = [[UIImageView alloc] initWithImage: 
                         [UIImage imageWithContentsOfFile: @"image1.png"]];

So in our code, we do it like this:

[[cell imageView] initWithImage: 
                         [UIImage imageWithContentsOfFile: @"image1.png"]];

(untested code)

see also: UIImage

Upvotes: 0

gnasher
gnasher

Reputation: 1513

Is your question how to make it appear in the table cell or how to read it from the resources folder?

To make it appear in the cell: cell.imageView.image = imageOfDogLoadedFromResourceFolder;

If the question is how to load from resources folder would need more information on exactly where in the file system your resources folder resides AND the format of the image file.

Cheers.

Upvotes: 0

Related Questions