Table Cell Class With Controlled Left Image

This example is based on a solution given here and allows the left image of a table cell to be customized.

In #ViewController.h

@end

//**************************************************
//**************************************************
//********** CLASS FOR BESPOKE TABLE CELL **********
//**************************************************
//**************************************************
@interface SizableImageCell : UITableViewCell {}
@end
In #ViewController.m

//*********************************
//***** DEFINE CELL FOR A ROW *****
//*********************************
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {
        cell = [[[SizableImageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] autorelease];

    }

...

@end

//**************************************************
//**************************************************
//********** CLASS FOR BESPOKE TABLE CELL **********
//**************************************************
//**************************************************
@implementation SizableImageCell
- (void)layoutSubviews
{
    [super layoutSubviews];

    float desiredWidth = 48;        //Image width
    float desiredHeight = 48;       //Image height
    float desiredMargin = 4;        //Margin requried left and above image
    float rightSideImageWidth = 0;  //0 unless a disclosure button is being used and you need to restrict the text width to allow for it
    float textLabelWidth = self.frame.size.width - (desiredMargin + desiredWidth + desiredMargin + rightSideImageWidth);

    //Note that detailTextLabel.frame.size.width is not the width of the available space but is actaully the width requried to display the label, so will be shorter than the avaialble width unless the label is too long.  When we increase the image area size we therefore don't substract the adjust amount from the width as this would truncate all labels, but we instead base the width on the total width avaiable.

    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.clipsToBounds = true;    

    self.imageView.frame = CGRectMake(desiredMargin, desiredMargin, desiredWidth, desiredHeight);

    self.textLabel.frame = CGRectMake((desiredMargin + desiredWidth + desiredMargin), self.textLabel.frame.origin.y, textLabelWidth, self.textLabel.frame.size.height);

    self.detailTextLabel.frame = CGRectMake((desiredMargin + desiredWidth + desiredMargin), self.detailTextLabel.frame.origin.y, textLabelWidth, self.detailTextLabel.frame.size.height);
}
@end
USEFUL?
We decided to help other designers and engineers by opening up many of our companies internal notes and libraries through mini sites like this. We benefit hugely from resources on the web and feel we should give back some of our knowledge and resources to the community. If you find this site helpful all we ask in return is that you consider adding a link back to any page on this site somewhere on the web. Link ins massively help towards this site appearing in search engine results, allowing other engineers to find it more quickly and of course giving us a bit of that sweet Google juice! Thanks.