I have a bunch of images I am using for cell's image views, they are all no bigger than 50x50. e.g. 40x50, 50x32, 20x37 .....
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.
The regular UITableViewCell works well to position things but the cell.imageView doesn't seem to behave like you want it to. I found that it's simple enough to get the UITableViewCell to lay out properly by first giving the cell.imageView a properly sized image like
Then you can just connect up your own properly working UIImageView with
Set the image on anImageView and it will do what you expect a UIImageView to do. Be the size you want it regardless of the image you give it. This should go in tableView:cellForRowAtIndexPath:
This solution essentially draws the image as 'aspect fit' within the given rect.
It's not necessary to rewrite everything. I recommend doing this instead:
Post this inside your .m file of your custom cell.
This should do the trick nicely. :]
image view add as a sub view to the tableview cell
Better create an image view and add it as a sub view to the cell.Then you can get the desired frame size.
This worked for me in swift:
Create a subclass of UITableViewCell (make sure you link up your cell in the storyboard)
In cellForRowAtIndexPath , dequeue the cell as your new cell type:
Obviously change number values to suit your layout