Note that there are some explanatory texts on larger screens.

plurals
  1. POSizing a Custom UITableViewCell and included UIImageView based on Image Size
    text
    copied!<p>I am trying to customize the UITableViewCell below for an iPhone app in a grouped table view. What I would like to do is have the image width take up the whole cell minus padding (280) and the height variable based on the image size.</p> <p>Currently I am using SDWebImage to asynchronously download remote images. This may not be the correct thing to do in this case. I am also having trouble figuring out how to give the custom cell the image on initialization. The image URL is stored in self.beerPhoto in the DetailViewController. </p> <p>I have searched for this a number of ways and have not found exactly what I am looking for. The closest was this: <a href="https://stackoverflow.com/questions/185652/how-to-scale-a-uiimageview-proportionally">How to scale a UIImageView proportionally</a>, but this method seems to require the cell to have the image at initialization, as I tried to make this code work but setting the image after initialization left a blank cell.</p> <p>The current code includes constants I set to approximate an image in portrait orientation. In reality some of the images are portrait and some are landscape orientation.</p> <p>Please let me know if there's anything additional you need to know.</p> <p>Header for custom UITableViewCell:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface BeerDetailHead : UITableViewCell { UILabel *beerName; UIImageView *beerImage; } @property(nonatomic, retain)UILabel *beerName; @property(nonatomic, retain)UIImageView *beerImage; @end </code></pre> <p>Relevant portion of implementation for custom UITableViewCell</p> <pre><code>#import "BeerDetailHead.h" @implementation BeerDetailHead @synthesize beerName, beerImage; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code //beerName = [[UILabel alloc]init]; //beerName.textAlignment = UITextAlignmentLeft; //beerName.font = [UIFont systemFontOfSize:14]; beerImage = [[UIImageView alloc]init]; //[self.contentView addSubview:beerName]; [self.contentView addSubview:beerImage]; } return self; } - (void)layoutSubviews { [super layoutSubviews]; CGRect contentRect = self.contentView.bounds; CGFloat boundsX = contentRect.origin.x; CGRect frame; frame= CGRectMake(boundsX+10 ,10, 280, 375); beerImage.frame = frame; } </code></pre> <p>DetailViewController's cellForRowAtIndexPath</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; NSArray *listData =[self.tableContents objectForKey: [self.sortedKeys objectAtIndex:[indexPath section]]]; NSLog(@"listData = %@", listData); NSUInteger row = [indexPath row]; if ([self.sortedKeys objectAtIndex:[indexPath section]] == @"header"){ static NSString *headerTableIdentifier = @"HeaderTableIdentifier"; BeerDetailHead * headerCell = (BeerDetailHead*)[tableView dequeueReusableCellWithIdentifier: headerTableIdentifier]; if(headerCell == nil) { headerCell = [[[BeerDetailHead alloc] initWithFrame:CGRectZero reuseIdentifier:headerTableIdentifier] autorelease]; } headerCell.beerName.text = [listData objectAtIndex:row]; [headerCell.beerImage setImageWithURL:[NSURL URLWithString:self.beerPhoto] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; //NSLog(@"frame = %@", headerCell.beerImage.frame); return headerCell; } else{ //use standard UITableViewCell } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload