2012-08-16 102 views
0

我有一个UITableView其单元格具有动态高度。我想要做的是基本上有一个图像作为每个tableview行的容器,如下图所示。由于每行的内容不同,因此应该调整图像的高度以包含单元格的文本。UIImage大小取决于UITableViewCell高度

Image container for cell

我想知道什么是实现这一目标的最佳实践。

在此先感谢

回答

3

对小区的背景图像查看和设置基础上,你必须在细胞内放置内容的帧大小。然后,您可以使用“stretchableImageWithLeftCapWidth:topCapHeight:”方法创建要设置单元格背景的图像。

UIImage *backgroundImg = [[UIImage imageNamed:@"test.png"] stretchableImageWithLeftCapWidth:30 topCapHeight:20]; 

[cell.bgImageView setFrame:CGRectMake(a, b, c, d]; 
cell.bgImageView.image = backgroundImg; 
+0

该方法在iOS 5中被弃用,因此您必须使用** resizableImageWithCapInsets:(UIEdgeInsets)capInsets **问题在于图像重复且不被拉伸。 – ubiAle 2012-08-16 13:28:45

+0

下面是我的代码:'UIEdgeInsets insets; insets.top = 20; insets.left = 0; insets.bottom = 20; insets.right = 0; UIImage * backgroundImg = [[UIImage imageNamed:@“container.png”] resizableImageWithCapInsets:insets]; cell.frame = CGRectMake(cell.frame.origin.x,cell.frame.origin.x,CELL_CONTENT_WIDTH,MAX(size.height,70.0f)); UIView * backgroundView = [[UIView alloc] initWithFrame:cell.frame]; cell.backgroundView = backgroundView; backgroundView.backgroundColor = [UIColor colorWithPatternImage:backgroundImg];' – ubiAle 2012-08-16 13:29:16

+0

使用UIimageView代替UIView作为背景视图,并将“backgroundImg”设置为UIImage视图而不设置颜色。 – Inoka 2012-08-16 13:35:07

0

哟可以使用方法sizeToFit这张图片。否则使用图像类型作为中心或scaleToFit。