Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何调整图像大小以适应UITableView单元格?_Ios_Objective C_Uitableview_Cocoa Touch_Uikit - Fatal编程技术网

Ios 如何调整图像大小以适应UITableView单元格?

Ios 如何调整图像大小以适应UITableView单元格?,ios,objective-c,uitableview,cocoa-touch,uikit,Ios,Objective C,Uitableview,Cocoa Touch,Uikit,如何将UIImage放入UITableView,UITableView单元格(?)的单元格中 在将子视图添加到单元格之前,是否有方法调整单元格.图像或UIImage的大小 我想保持默认的单元格大小(无论是什么,当你初始化为零矩形),并希望添加图标一样的图片到每个条目。图像略大于单元格大小(表行大小) 我认为代码是这样的(从我的头顶上看): 我需要做什么来调整图像大小以适应单元格大小? 我见过这样的情况: UIImageView * iview = [[UIImageView alloc] ini

如何将
UIImage
放入
UITableView
,UITableView单元格(?)的单元格中

在将
子视图添加到
单元格
之前,是否有方法调整
单元格.图像
UIImage
的大小

我想保持默认的单元格大小(无论是什么,当你初始化为零矩形),并希望添加图标一样的图片到每个条目。图像略大于单元格大小(表行大小)

我认为代码是这样的(从我的头顶上看):

我需要做什么来调整图像大小以适应单元格大小? 我见过这样的情况:

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];
    }
    

    重新缩放
    UIImage*
    时。您可以使用缩放来获得您正在使用的
    UIImage*
    的正确比例,从而将其缩小

    如果您可以访问apple devloper center,则会有一个关于缩放表格视图图像的视频。它更关注性能问题,并讨论了很多线程,但他确实展示了一些用于调整图像大小的示例代码

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

    - (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];
    }