在UITableViewCell中改变heightForRowAtIndexPath时,图像“移动”

时间:2022-07-02 20:25:27

I have a table view with varying height, as defined in the heightForRowAtIndexPath. For some very odd reason, the image is "indented" to the right based on the height; if the height is low enough, the image is stuck to the left side of the cell, but as the height increases, the image for said cell is shifted rightward compared to other rows.

我有一个不同高度的表视图,如heightForRowAtIndexPath中所定义。出于一些非常奇怪的原因,图像根据高度“缩进”到右边;如果高度足够低,则图像粘在单元格的左侧,但随着高度的增加,所述单元格的图像与其他行相比向右移动。

The result of this is a very poor looking list, with images floppily laid out in a zig-zag pattern depending on the height of each individual row.

结果是一个看起来非常糟糕的列表,图像根据每个单独行的高度以锯齿形图案轻柔地布置。

The problem is revealed by this simple example:

这个简单的例子揭示了这个问题:

- (CGFloat)tableView:(UITableView *)tableView 
  heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return (indexPath.row+1) * 50;
}

Each cell is set up (simplified) as a "Subtitle" style cell with:

将每个单元格设置(简化)为“字幕”样式单元格,其中:

// ...
cell.textLabel.text = @"foo";
cell.detailTextLabel.text = @"bar";
cell.imageView.backgroundColor = [UIColor redColor]; // for debugging; i have images with transparent bg
cell.imageView.image = anImageThatIs55x50pixelsBig;
return cell;

Any ideas? My head bleeds from the wall-love-affair.

有任何想法吗?我的头从墙上的爱情中流露出来。

Edit: uploaded a screen which displays this. The "image" is just a screenshot of a tiny area of the screen which makes it look a little weird, but you get the picture I'm sure: http://img28.imageshack.us/img28/549/screenshot20100311at172.png

编辑:上传了一个显示此内容的屏幕。 “图像”只是屏幕的一个小区域的屏幕截图,它看起来有点奇怪,但是你得到了我确定的图片:http://img28.imageshack.us/img28/549/screenshot20100311at172.png

2 个解决方案

#1


0  

What happens if you add your own UIImageView to the UITableViewCell instead of using the built in one? I believe the built in one is designed to add padding if there is room for it and ignore it if there is not.

如果您将自己的UIImageView添加到UITableViewCell而不是使用内置的UIImageView会发生什么?我相信内置的一个用于添加填充,如果有空间,如果没有则忽略它。

#2


0  

A potential cause could be the contentMode.

潜在的原因可能是contentMode。

UIView (a superclass of UIImageView) defines the property contentMode, which determines how a view lays out its content when its bounds rectangle changes. You may want to change this property to another mode, as in the case of an UIImageView it determines how and where an image is drawn.

UIView(UIImageView的超类)定义属性contentMode,该属性确定视图在其边界矩形更改时如何布置其内容。您可能希望将此属性更改为其他模式,因为在UIImageView的情况下,它确定图像的绘制方式和位置。

#1


0  

What happens if you add your own UIImageView to the UITableViewCell instead of using the built in one? I believe the built in one is designed to add padding if there is room for it and ignore it if there is not.

如果您将自己的UIImageView添加到UITableViewCell而不是使用内置的UIImageView会发生什么?我相信内置的一个用于添加填充,如果有空间,如果没有则忽略它。

#2


0  

A potential cause could be the contentMode.

潜在的原因可能是contentMode。

UIView (a superclass of UIImageView) defines the property contentMode, which determines how a view lays out its content when its bounds rectangle changes. You may want to change this property to another mode, as in the case of an UIImageView it determines how and where an image is drawn.

UIView(UIImageView的超类)定义属性contentMode,该属性确定视图在其边界矩形更改时如何布置其内容。您可能希望将此属性更改为其他模式,因为在UIImageView的情况下,它确定图像的绘制方式和位置。