在自定义 UITableViewCell 中调整 UIImageView 的大小会产生不一致的结果

Resizing UIImageView within custom UITableViewCell yielding inconsistent results(在自定义 UITableViewCell 中调整 UIImageView 的大小会产生不一致的结果)

本文介绍了在自定义 UITableViewCell 中调整 UIImageView 的大小会产生不一致的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于我在 XIB 文件中创建的 UITableViewCell 原型构建一个表.该单元由几个标签和一个图像视图组成,需要拉伸以适应整个设备宽度.一旦宽度被拉伸,我也想将高度拉伸到相同的比例,这样我的图像就会一致地缩放.

I'm building a table based on a UITableViewCell prototype I have created in a XIB file. The cell consists of a couple labels and an image view, which needs to be stretched to fit the full device width. Once the width is stretched, I also want to stretch the height to the same proportion, so my image is scaled consistently.

我得到的结果好坏参半.我已经能够使用以下代码适当地调整 CELL 的大小:

I'm getting mixed results. I've been able to get the CELL to size appropriately using the following code:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //pull the image from the array based on the indexPath
    NSUInteger row = [indexPath row];
    CITImage *image = [report.reportImages objectAtIndex:row];
    UIImage *img = [UIImage imageWithData:image.imageData];

    //see how we need to scale the image to make it fit within the full width of the screen
    float scale = tableView.frame.size.width /img.size.width;
    float imgHeight = img.size.height * scale;

    //return the necessary cell height
    return imgHeight;
}

问题是单元格内的 UIImageView 没有正确调整大小.我正在使用以下代码在准备绘制单元格时设置它的属性:

The problem is that the UIImageView within the cell is not resizing correctly. I'm using the following code to set the cell's properties when it's ready to be drawn:

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //pull the image from the array based on the indexPath
    NSUInteger row = [indexPath row];
    CITImage *image = [report.reportImages objectAtIndex:row];
    UIImage *img = [UIImage imageWithData:image.imageData];

    //attempt to get a reusable cell
    CITImageCell *cell;
    cell= [tableView dequeueReusableCellWithIdentifier:@"CITImageCellIdentifier"];
    if (cell == nil) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CITImageCell" owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
    }

    //see how we need to scale the image to make it fit within the full width of the screen
    float scale = tableView.frame.size.width /img.size.width;
    float imgHeight = img.size.height * scale;

    NSLog (@"Height %f",cell.imageView.frame.size.height);
    //size the image view
    cell.imageView.frame = CGRectMake(0,34, tableView.frame.size.width, imgHeight+34 );
    NSLog (@"Height %f",cell.imageView.frame.size.height);

    //assign the image
    cell.imageView.image = img;

    //return the cell ready to go
    return cell;

}

两个 NSLog 语句验证高度值计算是否正确,但是当图像显示在屏幕上时,它的大小很少正确.大多数时候它的大小与 XIB 文件中定义的大小相同,但有时它采用正确的高度.我还没有找到一个很好的模式.它从来不是表中的第一项,总是第二项,有时是第四和第五项.

The two NSLog statements verify that the height value is being calculated correctly, but when the image is displayed on-screen, it is RARELY sized correctly. MOST of the time it's the same size as defined in the XIB file, but SOMETIMES it takes on the correct height. I have not found a very good pattern. It's never the first item in the table, always the second, and sometimes the fourth and fifth.

仍在挖掘文档和其他人的故事,最让我感动的是我可以正确设置单元格大小,并且图像高度计算正确,但它仅在某些时候正确显示.

Still digging through documentation and other people's stories, and what gets me the most is that I can get the cell sized right, and that the image height is calculated right, but that it's only displayed right some of the time.

有什么帮助吗?显示我正在经历的截图的链接如下(是的,我昨晚正在观看辩论)

Any help? Links to screenshots showing what I'm experiencing are below (yes, I was watching the debates last night)

未缩放图像

正确缩放的图像

谢谢!

推荐答案

我遇到了同样的问题.经过一小时的工作,我意识到发生了什么!

I meet the same problem. And after one hour work, I realize what happened!

问题是 UITableViewCell 已经有一个 imageView!!

The Problem is that the UITableViewCell already has an imageView!!

如果你对 UIImageView 使用 SAME 属性名称 imageView,系统会以某种方式修改 imageView 你定义的.

And if you use the SAME property name imageView for the UIImageView, somehow the system will modify the imageView you defined.

所以解决方法是不要使用名称imageView,尝试使用其他名称.

So the solution is DONT use the name imageView, try to use other name.

例如:

@property (nonatomic, weak) UIImageView *myImageView;

至少对我有用.:)

这篇关于在自定义 UITableViewCell 中调整 UIImageView 的大小会产生不一致的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在自定义 UITableViewCell 中调整 UIImageView 的大小会产生不一致的结果

基础教程推荐