2009-05-18 155 views
6

如何將UIImage放入UITableView的單元格UITableViewCell(?)中。如何調整圖像大小以適應UITableView單元格?

addSubviewcell或者是有辦法來調整cell.imageUIImage之前它被分配給cell.image

我想保持單元格大小的默認值(不管它是什麼時候初始化爲零矩形),並且想要爲每個條目添加像圖片一樣的圖標。圖像略大於單元格大小(表格行大小)。

我認爲,代碼如下所示(從我的頭頂):

UIImage * image = [[UIImage alloc] imageWithName:@"bart.jpg"]; 
cell = ... dequeue cell in UITableView data source (UITableViewController ...) 
cell.text = @"bart"; 
cell.image = image; 

什麼我需要做的調整以適應單元格大小的圖像? 我見過類似的東西:

UIImageView * iview = [[UIImageView alloc] initWithImage:image]; 
iview.frame = CGRectMake(...); // or something similar 
[cell.contentView addSubview:iview] 

以上將增加圖像的細胞和不過,我可以計算出的大小,以適應它,:

  1. 我不知道是否有一個更好的 的方式,是不是太多開銷 加UIImageView只是爲了調整大小 cell.image
  2. 現在我的標籤(cell.text)需要 移動,因爲它是由圖像模糊, 我見過一個解決方案,您只需 添加的文本作爲標籤:

例子:

UILabel * text = [[UILable alloc] init]; 
text.text = @"bart"; 
[cell.contentView addSubview:iview]; 
[cell.contentView addSubview:label]; 
// not sure if this will position the text next to the label 
// edited original example had [cell addSubview:label], maybe that's the problem 

有人能指出我正確的方向嗎?

編輯:衛生署[cell.contentview addSubview:view][cell addSubview:view]也許我應該看看這個:

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = ...; 
    CGRect frame = cell.contentView.bounds; 
    UILabel *myLabel = [[UILabel alloc] initWithFrame:frame]; 
    myLabel.text = ...; 
    [cell.contentView addSubview:myLabel]; 
    [myLabel release]; 
} 
+0

沒有其他建議? – stefanB 2009-05-19 16:50:12

回答

2

This site posts code上重新調整一個UIImage*。您可以使用縮放比例來獲得正在使用的UIImage*的正確比率,​​以縮小比例。

+0

有趣的,我會看看,謝謝你的鏈接 – stefanB 2009-05-18 23:59:30

1

如果您有權訪問apple devloper中心,那麼有一個關於縮放表視圖的圖像的視頻。它更專注於性能問題並談論了很多關於線程的內容,但他確實展示了一些用於調整圖像大小的示例代碼。

視頻名稱:「有效的iPhone應用程序開發 - 第2部分」約30分鐘英寸

+0

謝謝,我會檢查出來 – stefanB 2010-08-01 23:52:02

相關問題