SeniorLee
SeniorLee

Reputation: 815

Fixing image-view in UITableViewCell in iPhone?

I have a table-cell which has all default settings. And I put an image on the ImageView on the left, and I put some random text on TextLabel.

and I adjust cell's height using below method.

The problem is the left ImageView's frame size is different depend on the cell's height.

See the below image, the 4th cell's height is bigger than others and it's image is slightly right compare to other images.

What causes this and how can I solve this probelm??

alt text

-- added

This is my cellForRowAtIndexPath method.


    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultt reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text = [self GetSelectionForRowAtIndexPath:indexPath];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"icon_notSelected" ofType:@"png"];
    UIImage *theImage = [UIImage imageWithContentsOfFile:path];
    cell.imageView.image = theImage;

This shows the imageView does not get bigger even if I put more height on the cell.

alt text

Upvotes: 1

Views: 1613

Answers (3)

James P
James P

Reputation: 4836

Give the imageview a fixed frame and then center it vertically in the cell.

Something along these lines:

imageview.frame = CGRectMake(5, floor(cellHeight/2)-20, 40, 40);

Or just use the imageview property of UITableViewCell.

Upvotes: 0

Dishant
Dishant

Reputation: 917

You can do it only by Code. No need of IB for this.

Upvotes: 0

Dishant
Dishant

Reputation: 917

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

put this code in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

If will show the image in each cell of the table in same line.

Upvotes: 0

Related Questions