Objective-c – How to make UITableViewCell’s ImageView a fixed size even when the image is smaller

iphoneobjective cuiimageviewuikituitableview

I have a bunch of images I am using for cell's image views, they are all no bigger than 50×50. e.g. 40×50, 50×32, 20×37 …..

When I load the table view, the text doesn't line up because the width of the images varies. Also I would like small images to appear in the centre as opposed to on the left.

Here is the code I am trying inside my 'cellForRowAtIndexPath' method

cell.imageView.autoresizingMask = ( UIViewAutoresizingNone );
cell.imageView.autoresizesSubviews = NO;
cell.imageView.contentMode = UIViewContentModeCenter;
cell.imageView.bounds = CGRectMake(0, 0, 50, 50);
cell.imageView.frame = CGRectMake(0, 0, 50, 50);
cell.imageView.image = [UIImage imageWithData: imageData];

As you can see I have tried a few things, but none of them work.

Best Answer

It's not necessary to rewrite everything. I recommend doing this instead:

Post this inside your .m file of your custom cell.

- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(0,0,32,32);
}

This should do the trick nicely. :]