即使图像较小,如何使 UITableViewCell 的 ImageView 成为固定大小 [英] How do I make UITableViewCell's ImageView a fixed size even when the image is smaller

查看:21
本文介绍了即使图像较小,如何使 UITableViewCell 的 ImageView 成为固定大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆用于单元格图像视图的图像,它们都不大于 50x50.例如40x50、50x32、20x37 .....

I have a bunch of images I am using for cell's image views, they are all no bigger than 50x50. e.g. 40x50, 50x32, 20x37 .....

当我加载表格视图时,文本没有对齐,因为图像的宽度不同.此外,我希望小图像出现在中心而不是左侧.

When I load the table view, the text doesn't line up because the width of the images varies. Also I would like small images to appear in the centre as opposed to on the left.

这是我在cellForRowAtIndexPath"方法中尝试的代码

Here is the code I am trying inside my 'cellForRowAtIndexPath' method

cell.imageView.autoresizingMask = ( UIViewAutoresizingNone );
cell.imageView.autoresizesSubviews = NO;
cell.imageView.contentMode = UIViewContentModeCenter;
cell.imageView.bounds = CGRectMake(0, 0, 50, 50);
cell.imageView.frame = CGRectMake(0, 0, 50, 50);
cell.imageView.image = [UIImage imageWithData: imageData];

如您所见,我尝试了一些方法,但都没有奏效.

As you can see I have tried a few things, but none of them work.

推荐答案

没有必要重写所有内容.我建议改为这样做:

It's not necessary to rewrite everything. I recommend doing this instead:

将此发布到自定义单元格的 .m 文件中.

Post this inside your .m file of your custom cell.

- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(0,0,32,32);
}

这应该可以很好地解决问题.:]

This should do the trick nicely. :]

这篇关于即使图像较小,如何使 UITableViewCell 的 ImageView 成为固定大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆