2012-08-16 106 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。