How do I make UITableViewCell's ImageView a fixed size even when the image is smaller
It's not necessary to rewrite everything. I recommend doing this instead:
Post this inside your .m file of your custom cell.
- (void)layoutSubviews { [super layoutSubviews]; self.imageView.frame = CGRectMake(0,0,32,32);}
This should do the trick nicely. :]
For those of you who don't have a subclass of UITableViewCell
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { [...] CGSize itemSize = CGSizeMake(40, 40); UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale); CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height); [cell.imageView.image drawInRect:imageRect]; cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [...] return cell;}
The code above sets the size to be 40x40.
Swift 2
let itemSize = CGSizeMake(25, 25); UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.mainScreen().scale); let imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height); cell.imageView?.image!.drawInRect(imageRect) cell.imageView?.image! = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
Or you can use another(not tested) approach suggested by @Tommy:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { [...] CGSize itemSize = CGSizeMake(40, 40); UIGraphicsBeginImageContextWithOptions(itemSize, NO, 0.0) [...] return cell;}
Swift 3+
let itemSize = CGSize.init(width: 25, height: 25)UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.main.scale);let imageRect = CGRect.init(origin: CGPoint.zero, size: itemSize)cell?.imageView?.image!.draw(in: imageRect)cell?.imageView?.image! = UIGraphicsGetImageFromCurrentImageContext()!;UIGraphicsEndImageContext();
The code above is the Swift 3+ version of the above.
Here's how i did it. This technique takes care of moving the text and detail text labels appropriately to the left:
@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;}