2

Is it possible to constraint the width of [[cell imageView] a standard UITableViewCell? I've tried [[cell imageView] setFrame:CGRectMake(0, 0, 50, 50)] but it has no effect.

[EDIT]

Otherwise, is it possible to change a UIImage size before adding it in the UIImageView?

Jesse
  • 8,605
  • 7
  • 47
  • 57
Nielsou Hacken-Bergen
  • 2,606
  • 6
  • 27
  • 37

6 Answers6

6

You can use .transform method in UIView example

cell.imageView.transform = CGAffineTransformMakeScale(0.65, 0.65);

//if you are subclassing UITableViewCell you can do this too

- (void)layoutSubviews {

    [super layoutSubviews];
    self.imageView.bounds = CGRectMake(10,5,65,65);
    self.imageView.frame = CGRectMake(10,5,65,65);
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;

    if (self.imageView.image) {
        //TODO: adjust the textLabel and detailTextLabel
    }

}
Chamira Fernando
  • 6,836
  • 3
  • 23
  • 23
  • 1
    After much looking around to resize an imageview or UIView withing a table cell use the cGAffineTransform methods. eg myUiImageView.transform = CGAffineTransformMakeScale(0.65, 0.65); subclassing and calling layout subviews did not work for me. – John Ballinger Dec 10 '13 at 02:50
  • setting a transform on a cell's image view may get reset if you are using dequeueReusableCellWithIdentifier: and the cell scrolls off the screen. – Ivan Lesko Jan 17 '14 at 21:46
2

If you can't simply set the image view's frame on a regular UITableViewCell, you could make a UITableViewCell subclass, and reposition the image view in -layoutSubviews.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
0

here is my swift solution of this problem

//
//  UITableVIewCellBasicWithFixedSizeImage.swift
//
//  Created by Andrew Ashurow on 4/28/16.
//  Copyright © 2016. All rights reserved.
//

class UITableVIewCellBasicWithFixedSizeImage: UITableViewCell {
        private let
        IMAGE_VIEW_WIDTH:CGFloat        = 27,
        TEXT_VIEW_LEFT_INSET:CGFloat    = 15

        override func layoutSubviews() {
            super.layoutSubviews()
            if let
                imageView = imageView,
                textLabel = textLabel{
                //set fixed image width
                imageView.frame = CGRect(
                    origin: imageView.frame.origin,
                    size: CGSize(
                        width: IMAGE_VIEW_WIDTH,
                        height: imageView.frame.size.height
                    )
                )
                imageView.contentMode = .Center
                imageView.autoresizingMask = .None

                //calculate textLabel inset
                textLabel.frame = CGRect(
                    origin: CGPoint(
                        x: imageView.frame.origin.x + imageView.frame.size.width + TEXT_VIEW_LEFT_INSET,
                        y: textLabel.frame.origin.y
                    ),
                    size: textLabel.frame.size
                )

            }
        }
    }
0x384c0
  • 2,256
  • 1
  • 17
  • 14
0

No it is not possible.

its width should not be changed, for that you have to create UITableViewCell subclass and you can change imageview in UITableViewCell and change width according to it. or u can use

UITableViewCell *cell = (UITableViewCell *)[tableView    dequeueReusableCellWithIdentifier:CellIdentifier];

UIImageView *imgView; 
if(cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

    imgView = [[UIImageView alloc] initWithFrame:(100,0,100,62)];
    [imgView setImage:[UIImage imageNamed:@"img.png"]];
    imgView.tag = 55;
    [cell.contentView addSubview:imgView];
    [imgView release];
} 
else
{
    imgView = (id)[cell.contentView viewWithTag:55];
}
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
PJR
  • 13,052
  • 13
  • 64
  • 104
0

Image Fix Size:

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[imgview setImage:image];
[cell addSubview:imgView];
[imgView release];
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54
0

This is the in-built functionality to provide imageView for every cell. Its frame cannot be changed unless it is sub-classed.

However, you can add any of your own imageViews by simply adding the following code in the method,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell == nil)
    {
        cell = (UITableViewCell*) [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        UIImageView* *myView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        [myView setImage:image];
        myView.tag = 123;

        [cell.contentView addSubview:myView];
        [myView release];
    }
    else
    {
        myView = (UIImageView*) [cell.contentView viewWithTag:123];
    }
}
Alex Zavatone
  • 4,106
  • 36
  • 54
mayuur
  • 4,736
  • 4
  • 30
  • 65