p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional iPhone and iPad Database Application Programming (http://p2p.wrox.com/forumdisplay.php?f=603)
-   -   Chapter 3 - Image ProductDetailViewController (http://p2p.wrox.com/showthread.php?t=86088)

nickpsaris December 19th, 2011 06:40 PM

Chapter 3 - Image ProductDetailViewController
 
How can you load the item image in the ProductDetailViewController?
Thanks

thepianoguy December 22nd, 2011 10:38 AM

Assuming I understand your question right, you want the image that is on the left in the tableCell of the rootViewController to also appear in the ProductDetailView. Two options that are available are:
1. add a UIImageView to the ProductDetailViewController.xib and as an IBOutlet in the ProductDetailViewController.h. (additions are bold)
@interface ProductDetailViewController : UIViewController {


IBOutlet UILabel* nameLabel;
IBOutlet UILabel* manufacturerLabel;
IBOutlet UILabel* detailsLabel;
IBOutlet UILabel* priceLabel;
IBOutlet UILabel* quantityLabel;
IBOutlet UILabel* countryLabel;
IBOutlet UIImageView *productImage;


}
Make the connection. In the ProductDetailViewController.m add the following (additions are bold)

-(void) setLabelsForProduct: (Product*) theProduct
{
// Set the text of the labels to the values passed in the Product object
[nameLabel setText:theProduct.name];
[manufacturerLabel setText:theProduct.manufacturer];
[detailsLabel setText:theProduct.details];
[priceLabel setText:[NSString stringWithFormat:@"%.2f",theProduct.price]];
[quantityLabel setText:[NSString stringWithFormat:@"%d",theProduct.quantity]];
[countryLabel setText:theProduct.countryOfOrigin];
NSString *filePath = [[NSBundle mainBundle] pathForResource:theProduct.image ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
productImage.image = image;

}
and option 2 would entail adding the UIImageView in code as a subview of the ProductDetailController.view rather than in the .xib and as an IBOutlet

2. In the ProductDetailViewController.m add the following (additions are bold)


-(void) setLabelsForProduct: (Product*) theProduct
{
// Set the text of the labels to the values passed in the Product object
[nameLabel setText:theProduct.name];
[manufacturerLabel setText:theProduct.manufacturer];
[detailsLabel setText:theProduct.details];
[priceLabel setText:[NSString stringWithFormat:@"%.2f",theProduct.price]];
[quantityLabel setText:[NSString stringWithFormat:@"%d",theProduct.quantity]];
[countryLabel setText:theProduct.countryOfOrigin];
UIImageView *prodImage = [[UIImageView alloc] initWithFrame:CGRectMake(200, 20, 100, 100)];
[self.view addSubview:prodImage];
NSString *filePath = [[NSBundle mainBundle] pathForResource:theProduct.image ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
prodImage.image = image;
[prodImage release];

}

Since the original project is "pre ARC" the retain/release pattern is used.
Hope this helps.

Bob

nickpsaris December 27th, 2011 05:56 PM

Thank you very much it work!!!!!!


All times are GMT -4. The time now is 12:15 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.