iphone - 即使图像较小,如何使 UITableViewCell 的 ImageView 成为固定大小

标签 iphone objective-c uitableview uiimageview uikit

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

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

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

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

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

最佳答案

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

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

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

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

关于iphone - 即使图像较小,如何使 UITableViewCell 的 ImageView 成为固定大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2788028/

相关文章:

ios - 调用 `tableView.reloadData()`后TableView重新加载失败

iphone - 直接从 Assets URL 读取

iphone - 在 iPhone 中捕获视频和图像

ios - 无法将自动布局约束添加到UITableViewCell中的元素

objective-c - 核心音频 : is zero equivalent to silence only for PCM audio?

ios - 这段代码是否泄漏内存(Objective-C 全局变量)?

ios - tableView.headerView 的高度(forSection : 0) is nil

ios - 删除时 CoreData 崩溃等等

iphone - 如何让我的应用程序更加 Shiny ?

iphone - 使用 NSPredicate 从 NSManagedObjectContect 获取准确的 NSDate