« Back to blog

Proper Alignment of UIImage and UILabel when using UITableViewCellStyleSubtitle

I am working on an iPhone App Project for a client and I decided to use the UITableViewCellStyleSubtitle for one of my views. It was easy to setup and it had all the details I needed. The only customization I did was change the detailTextLabel.numberOfLines to 2. So I loaded up my content and to my dismay, the cell is not intelligent enough to line up the labels horizontally across all cells. This really, really bothers me.

Mac_screenshot-1

Before I set out to create my own class and draw my own labels and images, I thought I would just subclass UITableViewCell and try overriding the layouts for what I needed.
 

 
Turns out that was all I needed. After using this subclass instead of a UITableViewCell, I now get my image filling the square (if possible) and the Text Labels all lined up vertically.

Mac_screenshot

This code makes 2 assumptions:
 
1) You want a square imageView that has a width equal to the height of the cell - 1 (for the separator)
2) You want the image to be aspect fit
 
Enjoy!

| Viewed
times | Favorited 0 times

5 Comments

Mar 17, 2010
Dale said...
Thank you for your code sample! I found it very helpful!
Jun 24, 2010
Oystein Saebo said...
Any way you can email me the whole Demo project? Struggling a bit to get a hold of the TableView...
Sep 12, 2011
Javier said...
Hey! I know it's been a while, but I could use that sample code too. Can you email it to me?
Thank you very much in advance,
Sep 12, 2011
Matt Coneybeare said...
I can't seem to locate the demo project… but all the pertinent code is written above in this post.
Sep 13, 2011
Javier said...
Hi, thank you for being so fast!
You're right, all the pertinent code was there! I just didn't understand how to use this subclass and found errors (mine, of course) while trying to do it. Thank God I came around with the proper way.

Leave a comment...