Created
April 21, 2011 06:18
-
-
Save chrishulbert/933839 to your computer and use it in GitHub Desktop.
Make a UITableViewCell with a resized image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface SizableImageCell : UITableViewCell {} | |
@end | |
@implementation SizableImageCell | |
- (void)layoutSubviews { | |
[super layoutSubviews]; | |
float desiredWidth = 80; | |
float w=self.imageView.frame.size.width; | |
if (w>desiredWidth) { | |
float widthSub = w - desiredWidth; | |
self.imageView.frame = CGRectMake(self.imageView.frame.origin.x,self.imageView.frame.origin.y,desiredWidth,self.imageView.frame.size.height); | |
self.textLabel.frame = CGRectMake(self.textLabel.frame.origin.x-widthSub,self.textLabel.frame.origin.y,self.textLabel.frame.size.width+widthSub,self.textLabel.frame.size.height); | |
self.detailTextLabel.frame = CGRectMake(self.detailTextLabel.frame.origin.x-widthSub,self.detailTextLabel.frame.origin.y,self.detailTextLabel.frame.size.width+widthSub,self.detailTextLabel.frame.size.height); | |
self.imageView.contentMode = UIViewContentModeScaleAspectFit; | |
} | |
} | |
@end | |
... | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
static NSString *CellIdentifier = @"Cell"; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (cell == nil) { | |
cell = [[[SizableImageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; | |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; | |
} | |
cell.textLabel.text = ... | |
cell.detailTextLabel.text = ... | |
cell.imageView.image = ... | |
return cell; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment