如何根据UIImageView高度设置不同的UITableViewCell高度?

时间:2016-01-07 12:01:12

标签: ios objective-c uitableview uiimageview

我需要根据uiimageview的高度scaledHeight变量设置Cells的高度 我在我的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)
    {

    }];

我如何在scaledHeightheightForRowAtIndexPath设置heightForRowAtIndexPath CellForRowAtIndexPath之前运行validates :number, length: { maximum: 20 }, if: :checksum_is_valid? def checksum_is_valid? if !Luhn.valid?(number) errors.add(number, "is not valid.") end end

5 个答案:

答案 0 :(得分:1)

-(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 :(得分:1)

<强> 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];
    }

答案 2 :(得分:1)

由于默认情况下异步加载图像,因此必须在heightForRowAtIndexPath方法中返回一些默认值。加载图像后,您只需重新加载该特定行。现在再次调用heightForRowAtIndexPath,您将返回图像的实际高度。您还可以在重新加载特定行时添加一些动画,以便UI过渡顺利。

答案 3 :(得分:0)

获取图像大小边界高度并将其返回到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;  
}

答案 4 :(得分:0)

默认情况下,使用一个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)
    {

    }];