[英]Set UIImageView height proportional to width in UITableViewCell
[英]How to set different UITableViewCell heights based on the UIImageView height?
我需要根據uiimageview的高度設置Cells的高度,這是scaledHeight
變量我從Parse獲取圖像就像在我的cellForRowAtIndexPath中一樣
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
之前CellForRowAtIndexPath
?
-(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.
}
1.嘗試在Array中設置默認高度
- (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方法中返回一些默認值。 加載圖像后,您只需重新加載該特定行。 現在再次調用heightForRowAtIndexPath,您將返回圖像的實際高度。 您還可以在重新加載特定行時添加一些動畫,以便UI過渡順利。
獲取圖像大小邊界高度並將其返回到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;
}
默認情況下,使用一個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)
{
}];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.