【问题标题】:How to resize image to fit UITableView cell?如何调整图像大小以适合 UITableView 单元格?
【发布时间】: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]

上面会将图像添加到单元格中,我可以计算尺寸以适应它,但是:

  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

有人能指点我正确的方向吗?

编辑: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


    【解决方案1】:

    This site posts code 重新调整UIImage*。您可以使用缩放来获得您正在使用的UIImage* 的正确比例,以缩小它。

    【讨论】:

      【解决方案2】:

      如果您有权访问苹果开发者中心,这里有一个关于缩放图像以用于表格视图的视频。它更专注于性能问题,并讨论了很多关于线程的问题,但他确实展示了一些用于调整图像大小的示例代码。

      视频名称:“有效的 iPhone 应用开发 - 第 2 部分”大约 30 分钟。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-21
        • 1970-01-01
        • 2012-05-24
        • 1970-01-01
        • 1970-01-01
        • 2013-02-27
        • 1970-01-01
        • 2017-04-10
        相关资源
        最近更新 更多