UITableViewCell imageView padding

I would like to set up fields or paddings for a UITableView cell imageView, how could I do this? With heightForRowAtIndexPath I can only set the height of the rows. And it just enlarges the image in the cell if I enlarge it.

Here is my cellForRow method

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease]; cell.textLabel.numberOfLines = 1; cell.textLabel.font = [UIFont systemFontOfSize:kSystemFontSize]; cell.accessoryType = UITableViewCellAccessoryNone; cell.textLabel.textColor = [UIColor whiteColor]; } cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row]; cell.imageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]]; cell.backgroundColor = [UIColor clearColor]; return cell; } 

Currently, I know only one way I could do this - compress the contents of the image images using Photoshop or a similar program while staying at the same time. But such a method will take a lot of time, and I think there should be an easier way to do this.

+7
source share
4 answers

Without subclass:

 cell.imageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, .5, .5); 

change '.5' with the right proportion for your case

+15
source

The best solution here is to create your own cell, add an image, labels inside the contentView and customize them as you want.

But there is another way: you need to subclass from UITableViewCell and override the layoutSubviews selector:

 - (void)layoutSubviews { [super layoutSubviews]; self.imageView.frame = CGRectMake(10, 10, 50, 50); self.imageView.contentMode = UIViewContentModeCenter; } 
+9
source

This works on iOS 7:

 - (void)layoutSubviews { [super layoutSubviews]; CGRect imageViewFrame = self.imageView.frame; imageViewFrame.origin.x = 10; imageViewFrame.origin.y += 5; imageViewFrame.size.height -= 10; imageViewFrame.size.width -= 10; self.imageView.frame = imageViewFrame; } 
+3
source

updated for swift3

 cell?.imageView?.transform = CGAffineTransform(scaleX: 0.6, y: 0.6) 
0
source

All Articles