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

查看:71
本文介绍了在自定义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!!

如果将 SAME 属性名称imageView用作UIImageView,则系统将以某种方式修改您定义的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的大小会产生不一致的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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