gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:02:36 25 4
gpt4 key购买 nike

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

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

- (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 大小调整不正确。当准备好绘制单元格时,我使用以下代码来设置单元格的属性:

-(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 文件中定义的大小相同,但有时它具有正确的高度。我还没有找到一个很好的模式。它从来不是表中的第一项,总是第二项,有时是第四项和第五项。

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

有什么帮助吗?下面是显示我所经历的屏幕截图的链接(是的,我昨晚观看了辩论)

Unscaled image

Correctly scaled image

谢谢!

最佳答案

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

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

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

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

例如:

@property (nonatomic, weak) UIImageView *myImageView;

至少为我工作。 :)

关于iphone - 在自定义 UITableViewCell 中调整 UIImageView 的大小会产生不一致的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13030632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com