我在TableViewCell中有不同的UIImageView高度,如何根据图像高度设置Cell的高度?

时间:2016-01-08 02:15:43

标签: ios objective-c uitableview uiimageview

目前在cellForRowAtIndexPath中显示uiimageview时,我计算图像的高度并将其存储在CGFloat scaledHeight

如何调整单元格大小并使其等于此高度?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {


    static NSString *CellIdentifier = @"PhotoCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    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]init];
            test.image = image;

            CGSize imgSize = image.size;
            CGFloat ratio = self.view.window.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)

    {
        NSLog(@"done");
    }];
.....

这是我的heightForRowAtindexPath(我的总数设置为500.0f,但有些图像的高度为250,所以还有很多额外的空间。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 500.0f;

}

0 个答案:

没有答案