【发布时间】:2010-10-27 04:33:45
【问题描述】:
如何将UIImage 放入UITableView 的单元格中,UITableViewCell (?)。
你是 addSubview 到 cell 还是有办法在分配给 cell.image 之前调整 cell.image 或 UIImage 的大小?
我想保持单元格大小为默认值(无论何时使用零矩形初始化),并且想为每个条目添加类似图片的图标。图片略大于单元格大小(表格行大小)。
我认为代码看起来像这样(从我的脑海中):
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
有人能指点我正确的方向吗?
编辑:Doh [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];
}
【问题讨论】:
标签: ios objective-c uitableview cocoa-touch uikit