• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:29:27 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我从不同大小的 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)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)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;
    }
}



Best Answer-推荐答案


希望对你有所帮助。

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)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;
}

欲了解更多信息,请参阅以下链接。

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

关于ios - 如何根据图像大小设置高度单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32443094/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap