views:

663

answers:

3

Hi,

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];
+1  A: 

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.
Williham Totland
See update, but basically I need to dynamically add the image, based on the [dog types] NSString.
Graeme
If you know what the [dog types] string will contain; why don't you just name your image file that? (So if you know the string will be, say, "dachshund", you can just pass it directly to imageName, which will produce "dachshund.png"
Williham Totland
That sounds OK, but any chance of some sample code? Not sure how to implement that. Thanks.
Graeme
@Graeme: Did you even read my answer? Oh well; let's try reediting it.
Williham Totland
Yeah I did, but I don't just have one image - ie There are 10 types of images I need to display depending on the [dog type] including labrador, dachshund, border collie and more. I get where that code is coming from, but mine needs to be dynamic.
Graeme
Only problem with that way is that [dog types] has a space in front of the name, and you can't do that in the image name. E.g. [Dog Types] may be [ Labrador ] and file can only be labrador.png.
Graeme
Use a dictionary where the key is the dog name and the object is the file name or UIImage of that dog. File name can be anything at that point. Are we getting close?
Kenny
@Graeme: Except that a) trimming whitespace is not only easy, it's a very good idea and b) file names can contain spaces. As in: `[[dog types] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];`
Williham Totland
Hmm still not working - any ideas?
Graeme
Never mind - got it working. Thanks!
Graeme
A: 

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.

Kenny
See update, but basically I need to dynamically add the image, based on the [dog types] NSString.
Graeme
Do you mean Lazy Load? It looks like you have everything you need with that sample code. What is the behaviour you have and the behaviour you want. That might help.
Kenny
Basically I have an NSArray from which I call data using [dog types] in the UITableView. What I want is for each cell to display an image based on this [dog types] string which matches what the image is called. E.g. [dog types] string may say "labrador", so a labrador picture would display. In the cell below, [dog types] may contain a string which says "border collie", and a picture of a Border Collie shows. Note that these are parsed from an RSS feed, so they change in order all the time.
Graeme
A: 

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

Efrain