AsTheWormTurns
AsTheWormTurns

Reputation: 1316

Inserting image in UITableView cell issue

sorry for the silly question, but I'm still in the process of learning Objective-C and iPhone programming.

I'm trying to insert an icon to the left of a UITableView cell. This is from the method (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath :

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"icon3" ofType:@"png"];
UIImage *icon = [UIImage imageWithContentsOfFile: imagePath];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.image = icon;

My icon (30w x 44h) MUST be as high as the cell, while at the moment it remains a little space (maybe 1 pixel) above it and below. I tried to change the dimensions of the image with no success and I tried to reduce the height of the cell (with no effect) with (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath.

What can I do?

Eventually I should insert a little space on the left too, but this is secondary.

Thanks very much.

This is the screenshot: check that white pixel above the yellow icon

[UPDATE] no problem when the UITableView is not "grouped"

Upvotes: 1

Views: 634

Answers (3)

Syed Faraz Haider Zaidi
Syed Faraz Haider Zaidi

Reputation: 1367

Based on code from CanadaDev here:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5,5,20,20)]; // set the location and size of your imageview here.
imageView.image = [UIImage imageNamed:@"image.png"]; // set the image for the imageview
imageView.highlightedImage = [UIImage imageNamed:@"imageHighlighted.png"]; // if you need it, this will allow you to create an image used when the cell is highlighted. For example, a white copy of your image to match the white text.
[cell addSubview:imageView];

and then for text:

UILabel *price=[[UILabel alloc]initWithFrame:CGRectMake(80,60, 180,12)];
price.text=[NSString stringWithFormat:@"price %@",temp];
price.backgroundColor=[UIColor clearColor];
[price setFont:[UIFont fontWithName:@"American Typewriter" size:12]];
[cell.contentView addSubview:price];
[price release];

Upvotes: 2

tobiasbayer
tobiasbayer

Reputation: 10369

Try to adjust the size of cell.imageView.

cell.imageView.bounds = CGRectMake(0,0,0,0) //fill in appropriate location and size for the zeros

Upvotes: 0

Craig Mellon
Craig Mellon

Reputation: 5399

You need to change the height of the row for the cell.imageView to be resized.

implement the following method and return the height of your image.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

Try also removing the contentMode setting in your cellForRowAtIndexPath method. You can replace all your code with one line as below:

cell.imageView.image = [UIImage imageNamed:@"icon3.png"];

Upvotes: 3

Related Questions