我遇到了这样的问题,我在一个表中存放了一个单元格 - uilabel和uiimageview。随着服务器图像有不同的大小 - 正方形或矩形,我让它们挤压屏幕尺寸(收缩宽度和高度自动计算)。在我的HIB文件中,uiimageview像我一样固定255的高度,以便计算元素uilabel与动态图像高度的距离,而不是XIB中列出的距离。我读了几本指南,但不知道错误在哪里。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat size = [self heightForImageCellAtIndexPath:indexPath];
NSLog(@"height cell = %f", size);
return size;
}
- (CGFloat)calculateHeightForConfiguredSizingCell:(UITableViewCell *)sizingCell {
[sizingCell setNeedsLayout];
[sizingCell layoutIfNeeded];
CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height + 1.0f; // Add 1.0f for the cell separator height
}
- (CGFloat)heightForImageCellAtIndexPath:(NSIndexPath *)indexPath {
static HomeTableViewBigCell *sizingCell = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sizingCell = [self.tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeTableViewBigCell class])];
});
NSLog(@"ok 1");
[self configureImageCell:sizingCell atIndexPath:indexPath];
return [self calculateHeightForConfiguredSizingCell:sizingCell];
}
- (void)configureImageCell:(HomeTableViewBigCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Article *article = [self.articles objectAtIndex:indexPath.row];
cell.titleLabel.text = article.title;
cell.subtitleLabel.text = article.subtitle;
cell.thematicsLabel.text = @"CUSTOM";
NSString *path = [NSString stringWithFormat:@"%@/%@",[RestApiRoutes getBaseURL], article.imagePath];
NSURL *fullImagePath = [NSURL URLWithString:path];
[cell.imageView sd_setImageWithURL:fullImagePath
placeholderImage:self.placeholder options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
cell.imageView.image = [self resizeImage:image :CGSizeMake(self.view.frame.size.width, 0)];
}];//0 - dynamic height
}
HomeTableViewBigCell.m中的:
-(void)layoutSubviews {
[super layoutSubviews];
[self.contentView updateConstraintsIfNeeded];
[self.contentView layoutIfNeeded];
CGRect contentImageFrame = self.imageView.frame;
contentImageFrame.origin.y = 0;
contentImageFrame.origin.x = 0;
self.imageView.frame = contentImageFrame;
}
非常想了解如何制作计算距离图像新高度的距离而不是严格预定距离的较低元素。感谢名单!