Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何根据图像大小设置高度单元格_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 如何根据图像大小设置高度单元格

Ios 如何根据图像大小设置高度单元格,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我从不同大小的URL获取图像。我根据图像高度设置了单元格高度 [cell.imgshow setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strImageUrl]] placeholderImage:[UIImage imageNamed:@"preloader_img_1.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *respo

我从不同大小的URL获取图像。我根据图像高度设置了单元格高度

[cell.imgshow setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strImageUrl]] placeholderImage:[UIImage imageNamed:@"preloader_img_1.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
    cell.imgshow.image = image;
    imageStatus =@"Load";

    if (image != nil) {
        // [images insertObject:image atIndex:indexPath.row];
        [images setObject:image forKey:[NSString stringWithFormat:@"%li,%li",(long)indexPath.row,(long)indexPath.section]];
        NSLog(@"%@",images);
        [tblDropDown reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                              withRowAnimation:UITableViewRowAnimationNone];
    }
这是我用于图像加载和引用代码的代码

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIImage *image = (UIImage *)[images objectForKey:
                                 [NSString stringWithFormat:@"%li,%li",
                                  (long)indexPath.row,(long)indexPath.section]];

    if (image != nil)
    {
        return image.size.height+300;
    }
    else{
        return 300.0;
    }
}

填充
UITableView
时,
DataSource
方法heightforrowatinexpath在
cellforrowatinexpath
之前执行,因此首先必须从
heightforrowatinexpath
返回单元格的适当高度,您无法从
单元格中设置UITableViewCell的高度,以便于在索引路径中设置UITableViewCell的高度,希望这对您有所帮助

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

    ...

    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
           placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                    success:^(UIImage *image, BOOL cached) {

                        // save height of an image to some cache
                        [self.heightsCache setObject:[NSNumber numberWithFloat:imHeight] 
                                              forKey:urlKey];

                        [tableView beginUpdates];
                        [tableView reloadRowsAtIndexPaths:@[indexPath]
                                         withRowAnimation:UITableViewRowAnimationFade];
                        [tableView endUpdates];
                    }
                    failure:^(NSError *error) {... failure code here ...}];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // try to get image height from your own heights cache
    // if its is not there return default one
    CGFloat height = [[self.heightsCache objectForKey:urlKeyFromModelsArrayForThisCell] floatValue];
    ...
    return newHeight;
}
有关更多信息,请参阅以下链接


您是否尝试过通过自动布局而不是代码来完成以下操作?您有什么问题?你发布了一些代码,但没有说明问题是什么。我必须根据图像大小设置单元格高度。你发布的代码应该这样做,那么你对代码有什么问题?我无法根据图像大小设置单元格高度,而不仅仅是发布没有解释的代码。怎么了?你的答案如何解决这个问题?@rmaddy我不会发布答案来满足你的需要。