如何將UIImage
放入UITableView
的單元格UITableViewCell(?)中。如何調整圖像大小以適應UITableView單元格?
你addSubview
到cell
或者是有辦法來調整cell.image
或UIImage
之前它被分配給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]
以上將增加圖像的細胞和不過,我可以計算出的大小,以適應它,:
- 我不知道是否有一個更好的 的方式,是不是太多開銷 加
UIImageView
只是爲了調整大小cell.image
? - 現在我的標籤(
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];
}
沒有其他建議? – stefanB 2009-05-19 16:50:12