Newbee
Newbee

Reputation: 3301

How to set fixed size for UITableViewCell's ImageView?

For my application's contact view, I have added individual users image based on his email id in an table view. Because of difference in image size of each user, image view is not unique for all user, some image view's width is more, So I set the frame size of UItableViewCell with fixed width, still the width size varies.

 [cell.imageView setFrame:CGRectMake(0, 0, 55, 55)];

What should I do? Do I need to add new image view as subview for cell? Any idea?

Upvotes: 1

Views: 14012

Answers (4)

Niru Mukund Shah
Niru Mukund Shah

Reputation: 4733

You should set your imageview's property in order to display different images for different size.

For Example,

<imageview_name>.contentMode = UIViewContentModeScaleAspectFit;

Enjoy Programming!

Upvotes: 1

mhrrt
mhrrt

Reputation: 977

Subclass UITableViewCell and override this method;

- (void)layoutSubviews {

    [super layoutSubviews];

    self.imageView.frame = CGRectMake(0,0,55,55);

    //Any additional setting that you want to do with image view

    [self.imageView setAutoresizingMask:UIViewAutoresizingNone];

}

Upvotes: 0

Luis
Luis

Reputation: 447

I'm using the same JSON file to fill a UITableView, and here is what I'm using for a universal app in CustomCell:

- (void)layoutSubviews {
    [super layoutSubviews];
    if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad) {
        self.imageView.frame = CGRectMake(0,0,300,100);

    }else if  (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
        self.imageView.frame = CGRectMake(0,0,180,60);
    }
    float limgW =  self.imageView.image.size.width;
    if(limgW > 0) {

        if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad) {
            self.textLabel.frame = CGRectMake(320,self.textLabel.frame.origin.y,self.textLabel.frame.size.width,self.textLabel.frame.size.height);

        }else if  (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
            self.textLabel.frame = CGRectMake(182,self.textLabel.frame.origin.y,self.textLabel.frame.size.width,self.textLabel.frame.size.height);
        }

    }
}

Upvotes: 0

bateristt
bateristt

Reputation: 176

UITableView's imageView property is read-only. You can resize your image before setting to your cell's imageView. To resize the image, you can get help from this StackOverflow answer.

Also, if you are loading your images from a web environment, you can use my fork of AFNetworking, I added image resizing options into the UIImageView extensions. https://github.com/polatolu/AFNetworking

Upvotes: 2

Related Questions