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

时间:2015-09-07 16:46:56

标签: ios objective-c uitableview

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

[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;
    }
}

2 个答案:

答案 0 :(得分:1)

希望这对你有帮助。

$ heroku run rails console
>> REDIS.set("answer", 42)
"OK"
>> REDIS.get("answer")
42

有关详细信息,请参阅以下链接。

https://github.com/jurezove/dynamic-uitableviewcells/tree/master

答案 1 :(得分:0)

DataSource方法heightForRowAtIndexPath在cellForRowAtIndexPath填充UITableView之前执行,所以首先你需要从heightForRowAtIndexPath返回一个适当的单元格高度,你不能从cellForRowAtIndexPath设置UITableViewCell的高度。

相关问题