何使UITableViewCell的ImageView成为固定大

何使UITableViewCell的ImageView成为固定大

本文介绍了即使图像较小,如何使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成为固定大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 23:11