如何根据 UIImageView 高度设置不同的 UITableViewCell 高度?
Posted
技术标签:
【中文标题】如何根据 UIImageView 高度设置不同的 UITableViewCell 高度?【英文标题】:How to set different UITableViewCell heights based on the UIImageView height? 【发布时间】:2016-01-07 12:01:12 【问题描述】:我需要根据 uiimageview 的高度(scaledHeight
变量)设置单元格的高度
我在我的 cellForRowAtIndexPath 中从 Parse 获取图像
PFFile *userImageFile = object[@"image"];
[userImageFile getDataInBackgroundWithBlock:^(NSData * _Nullable data, NSError * _Nullable error)
if (!error)
// UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
UIImage *image = [UIImage imageWithData:data];
// imageView.image = image;
UIImageView *test = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 400, 320)];
test.image = image;
CGSize imgSize = image.size;
CGFloat ratio = test.frame.size.width/imgSize.width;
CGFloat scaledHeight = imgSize.height * ratio;
[test setFrame:CGRectMake(0, 0, self.view.window.frame.size.width, scaledHeight)];
NSLog(@"HEIGHT:%f",scaledHeight);
[cell addSubview:test];
progressBlock:^(int percentDone)
];
如何在heightForRowAtIndexPath
中设置scaledHeight
,因为heightForRowAtIndexPath
在CellForRowAtIndexPath
之前运行?
【问题讨论】:
【参考方案1】:由于您默认异步加载图像,因此您必须在 heightForRowAtIndexPath 方法中返回一些默认值。加载图像后,您只需重新加载该特定行。现在再次调用 heightForRowAtIndexPath ,您将返回图像的实际高度。您还可以在重新加载特定行时添加一些动画,以便 UI 过渡平滑。
【讨论】:
怎么做的例子? tableView.reloadRowsAtIndexPaths([NSIndexPath.init(forItem: 0, inSection: 0)], withRowAnimation: .Automatic)【参考方案2】:1.尝试在数组中设置默认高度
- (void)setDefaultRowHeights
self.imageHeights = [NSMutableArray arrayWithCapacity:self.imageURLs.count];
for (int i = 0; i < self.imageURLs.count; i++)
self.imageHeights[i] = @(self.tableView.rowHeight);
2。在 cellForRowAtIndexPath 中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *imageURL = self.imageURLs[indexPath.row];
__weak TableViewCell *weakCell = cell;
__weak typeof(self) weakSelf = self;
[cell.mainImageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]]
placeholderImage:[UIImage new]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
weakCell.mainImageView.image = image;
// Calculate Heights
NSInteger oldHeight = [weakSelf.imageHeights[indexPath.row] integerValue];
NSInteger newHeight = (int)image.size.height;
// Update table row height if image is in different size
if (oldHeight != newHeight)
weakSelf.imageHeights[indexPath.row] = @(newHeight);
[weakSelf.tableView beginUpdates];
[weakSelf.tableView endUpdates];
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
NSLog(@"Error:");
];
return cell;
3.在 heightForRowAtIndexPath 中
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return [self.imageHeights[indexPath.row] integerValue];
【讨论】:
这不起作用,因为 heightForRowAtIndexPath 在 CellforRowAtIndexPath 之前加载,我在 cellForRowAtIndexPath 中下载图像【参考方案3】:-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//This is for same height of image view
uitableViewCell *cell = [tableView cellforRowAtIndexPath : indexPath];
return cell.imageView.size.height;
//If you have different sizes of images then store all those image in array and then calculate the height.
【讨论】:
如何计算heightForRow..
内的图像高度
imageView.size.height 会给你高度。
如果在 cellForRow 中设置了 imageView,我该怎么做?
这意味着我必须将其子类化为自定义单元格?【参考方案4】:
默认取一个 mutablearray(NSMutableArray *arrayHeights
) 这个数组的对象数等于总行数。他们都设置了“0”值
和heightForRowAtIndexPath
返回
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return [arrayHeights[indexPath.row] floatValue] + 50.0; // 50 is default space set it according your requirements
现在,当您从 getDataInBackgroundWithBlock 获得结果时,将 arrayHeights 的对象替换为该特定索引的图像高度并重新加载该单元格
PFFile *userImageFile = object[@"image"];
[userImageFile getDataInBackgroundWithBlock:^(NSData * _Nullable data, NSError * _Nullable error)
if (!error)
// UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
UIImage *image = [UIImage imageWithData:data];
// imageView.image = image;
UIImageView *test = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 400, 320)];
test.image = image;
CGSize imgSize = image.size;
CGFloat ratio = test.frame.size.width/imgSize.width;
CGFloat scaledHeight = imgSize.height * ratio;
[arrContactList replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithFloat:scaledHeight]];
[test setFrame:CGRectMake(0, 0, self.view.window.frame.size.width, scaledHeight)];
NSLog(@"HEIGHT:%f",scaledHeight);
[cell addSubview:test];
// reload cell
progressBlock:^(int percentDone)
];
【讨论】:
【参考方案5】:获取图像大小边界高度并将其返回到 heightForRowAtIndexPath。这将根据您的图像高度大小动态地改变您的高度。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//Assign your url
NSURL* aURL = [NSURL URLWithString:@"URL"];
//get data of that url image.
NSData* data = [[NSData alloc] initWithContentsOfURL:aURL];
//get image
UIImage *image = [UIImage imageWithData:data];
//return it's bounding height (bounds return the area of height image requires.)
return image.bounds.height;
【讨论】:
以上是关于如何根据 UIImageView 高度设置不同的 UITableViewCell 高度?的主要内容,如果未能解决你的问题,请参考以下文章
根据 iOS SWIFT 高度更高的 UIImageView 或 UILabel 设置 UITableViewCell 高度
如何根据 UIImageView 的高度估计 UITableViewCell 的高度
我需要设置 UITableViewCell 动态高度,带有 UIImageView 的多个动态标签