0

我创建了一个自定义 UITableViewCell 类。在那个类中,我根据里面的图像调整单元格的高度。

- (void)layoutSubviews {

    [super layoutSubviews];

    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    if (self.imageView.image) {
        self.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y,             
        self.frame.size.width, self.imageView.image.size.height);
    }
}

但由于它们都是不同的高度,一些单元格相互重叠。

在此处输入图像描述

第一个图像的高度与默认单元格高度相同,因此第二个图像很好。第二个图像具有较大的高度,这使得第三个图像位于其后面。

我可以以某种方式从 layoutSubviews 访问前一个单元格并从中调整 y 位置吗?

4

2 回答 2

1

通过这样做layoutSubviews而不是tableView:heightForRowAtIndexPath:,您的代码依赖于副作用并试图击败UITableView。您应该解决这个问题tableView:heightForRowAtIndexPath:并让 UITableView 为您完成工作。

于 2013-09-26T10:46:40.310 回答
0

而不是在方法中固定一个恒定的行高

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65; //For example

}

例如,如果您有一个包含有序图像名称的数组 (imageNamesArray),则可以使用如下内容:

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat desiredImageWidth = 50.0f;

    UIImage *image = [UIImage imageNamed:[imageNamesArray objectAtIndex:indexPath.row]];

    CGFloat ratio = image.size.height/image.size.width;

    return desiredImageWidth * ratio;

}

于 2013-09-26T11:13:17.600 回答