简体   繁体   中英

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?

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

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

For Example,

<imageview_name>.contentMode = UIViewContentModeScaleAspectFit;

Enjoy Programming!

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];

}

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);
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM