UITableViewCell imageView填充

我想为UITableView单元格imageView设置边距或填充,我怎么能做到这一点? 使用heightForRowAtIndexPath我只能设置行高。 如果我增加,它只是放大单元格中的图像。

这是我的cellForRow方法

 - (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; } 

目前,我只知道一种方法,我可以做到这一点 – 缩小图像的图像内容使用Photoshop或类似的程序,同时停留在同一时间。 但是这样的方法会花费很多时间,我想应该有更简单的方法来做到这一点。

没有子类:

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

改变你的情况下正确的比例“.5”

这里最好的解决scheme是创build自己的单元格,在contentView添加图像,标签,并调整它们的方式。

但也有另一种方法,你需要从UITableViewCell创build一个子类并重写layoutSubviewsselect器:

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

这适用于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; }