Adding dynamic image to UITable View Cell
I have a table where I want a dynamic image to load from the left side. The table should use an IF statement to select the appropriate image from the Resources folder and should be based on [dog types].
[dog types] is retrieved from an RSS feed, so the image in the table cell must match each cell's [dog types] tag.
Update: See the code below for what I'm looking for (just below for earthquakes, for me its dog photos).
I suspect I need to use - (UIImage *) imageForTypes: (NSString *) types {to do such a thing.
Thanks.
Updated code: This is an Apple Earthquake example, but it does exactly what I need to do. It matches images with severity (2.0, 3.0, 4.0, 5.0, etc.) from every earthquake to 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 below - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
magnitudeImage.image = [self imageForMagnitude:earthquake.magnitude];
a source to share
Image is set to
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You might want to use this method for brevity, but you just set the image the same way you set the static one.
Edit: That is; in this particular method:
cell.imageView.image = [UIImage imageNamed:@"Dachshund"]; // Dachshund.jpg exists in the resources directory, of course.
Redo: To be 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.
a source to share
Is your question how to make it appear in a table cell or how to read it from the resource folder?
To make it appear in the cell: cell.imageView.image = imageOfDogLoadedFromResourceFolder;
If the question is how to load from the resource folder, you will need more information about where exactly the resource folder is located in the file system and the format of the image file.
Greetings.
a source to share
I've never used this, but I think you can use the imageView
table cell property ( UITableViewCell ) to add images:
@property(nonatomic, readonly, retain) UIImageView *imageView
It is a pointer to a UIImageView that is created (assigned) by the cell itself (which is why it is read-only). Typically, we can load an image into a UIImageView like this using 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
a source to share