ios - 从服务器下载图像后动态制作表格 View 单元格高度

标签 ios swift uitableview uitableviewautomaticdimension

我有一个单元格,我现在在其中显示从服务器下载的图像。现在我显示静态高度的图像,但现在我想像 facebook 和 instagram 一样动态。例如,如果我上传图像 320 * 100 那么我的单元格需要变成 300 * 100。如果需要,还建议服务器端更改 我正在使用 afnetworking 图像加载类。

这是我的单元格设计。 enter image description here

编辑: 我尝试了给定的解决方案,但现在的问题是单元格在第二次进入 cellforindexpath 方法时会突然调整大小。这只会发生在第一个单元格中。

最佳答案

我做过类似的任务,在表格上显示图像并调整 tableview 单元格的大小,以便图像沿全屏宽度显示

IndexPath 处行的高度

var cachedHeight = [IndexPath : CGFloat]()
    override func tableView(_ tableView: UITableView, heightForRowAtindexPath: IndexPath) -> CGFloat {

    let default_height : CGFloat = 332.0

    // lookup for cached height
    if let height = cachedHeight[indexPath]{
        return height
    }

    // no cached height found
    // so now try for image so that cached can be calculated and cached
    let cache : SDImageCache = SDImageCache.shared()
    let image : UIImage? = cache.imageFromDiskCache(forKey: self.viewModel?.getProductImage(of: indexPath.row, at: 0))

    if let image = image{
        let baseHeight : CGFloat = CGFloat(332 - 224)   // cell height - image view height
        let imageWidth = self.tableView.frame.size.width
        let imageHeight = image.size.height * imageWidth / image.size.width

        cachedHeight[indexPath] = imageHeight + baseHeight
        return cachedHeight[indexPath]!
    }

    //
    //  even if the image is not found, then
    //  return the default height
    //
    return default_height
}

IndexPath 处行的单元格

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let sdCache = SDImageCache.shared()
    let cell = tableView.dequeueReusableCell(withIdentifier: "MyXYZCell", for: indexPath) as! AuctionListTableViewCell
    let imageURL = self.viewModel?.getProductImage(of: indexPath.row, at: 0)

    if let imageURL = imageURL {
        if (sdCache.imageFromCache(forKey: imageURL) != nil) {
            //
            //  image data already persist on disk,
            //  cell height update required
            //
            cell.auctionImageView.sd_setImage(with: URL.init(string: imageURL), completed: nil)
        }
        else
        {
            cell.auctionImageView.sd_setImage(with: URL.init(string: imageURL), completed: { (image, err, cacheType, url) in
                self.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.none)
            })
        }
    }

    //other cell customization code

    return cell
}

我用过SDWebImage .你可以使用它,或者在 AFNetworking 中找到类似的 api。

主题演讲:

  1. 此处cachedHeight用于缓存IndexPath索引的单元格高度,因为从磁盘读取图像是安静的I/O耗尽任务,结果表格 View 滚动滞后。
  2. heightForRow中检查,图片是否在缓存中,如果在缓存中,则计算高度,将其存储到cachedHeight中,否则返回默认高度. (我的默认高度是为我的占位符图像计算的)
  3. cellForRowAtIndexPath 中,我检查过图像是否在缓存中。如果图像在缓存中,则不需要重新加载,因为已经计算了高度。否则我尝试网络获取,当获取完成时我请求 tableview 重新加载该单元格。

希望对您有所帮助,编码愉快。

关于ios - 从服务器下载图像后动态制作表格 View 单元格高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49851346/

相关文章:

ios - React Native 如何强制重新加载应用程序或 Root View

iphone - UIWebView 到 View Controller ?

ios - 如何根据在 UITextView 中输入的文本更新 UITableViewCell 高度

Swift:CountableRange<Int> 数组:传递给不带参数的调用的参数

ios - 相同的代码在一个项目中有效,在另一个项目中无效

ios - UITableView动态单元格高度性能

ios - 分页直到结束

ios - 如何在我的每个 UITableView 单元格中检索 UISwitch 状态?

multithreading - 使用 UIButton 按下打破 Swift do-while 循环是否需要一个新线程?

ios - 在 travis-ci 中跳过在本地运行的测试