【问题标题】:Resizing UITableViewCell after loading image加载图像后调整 UITableViewCell 的大小
【发布时间】:2017-10-03 09:00:51
【问题描述】:

我正在使用动态高度表视图单元格和 Haneke 使用以下代码下载和缓存图像:

override func viewDidLoad() {
    super.viewDidLoad()
    myTableView.rowHeight = UITableViewAutomaticDimension
    myTableView.estimatedRowHeight = 500
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:ImageCellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ImageCellTableViewCell;

    cell.myImageView.hnk_setImage(from: URL(string: imageUrl), placeholder: nil)
    return cell
}

这里我需要在下载后设置图像时调整单元格高度。 下载图像后如何调整表格视图单元格的大小。

【问题讨论】:

    标签: ios swift uitableview uitableviewautomaticdimension


    【解决方案1】:

    只需为您的图像添加适当的约束,例如,使用纵横比约束的顶部和左侧约束。(您甚至可以在情节提要界面中执行此操作)

    【讨论】:

    • 我已经完成了。我的问题是,第一次单元格高度将根据约束(下载前),下次滚动到同一行时,高度将根据到图像高度(下载后)。我希望第二个高度自动反映在单元格中。
    【解决方案2】:

    打电话就行了

    tableView.beginUpdates() tableView.endUpdates()

    下载图片并设置高度限制后。希望这会有所帮助!

    编辑:

    另一个选项,为单元格提供正确的高度,是从服务器接收图像大小。这样您就可以用刚刚计算出的单元格高度来喂表。

    【讨论】:

    • 但是对每个单元格图像执行此操作会使滚动不连贯。
    • 当然,这有它的缺点。我将在答案中发布另一个建议。
    • 从您的代码中,我以为您的表格中只有一个单元格))
    • 可以从服务器获取图片大小吗?
    • 不,实际上我是从网页中获取图像 url。例如,我在我的应用程序中发布了一些网络链接内容,然后我将从该特定站点获取缩略图图像 url。
    【解决方案3】:

    设置表格视图的行高,如:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:ImageCellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ImageCellTableViewCell;
    
        cell.myImageView.hnk_setImage(from: URL(string: imageUrl), placeholder: nil)
        tableView.rowHeight = cell.myImageView.frame.origin.y + cell.myImageView.frame.size.height + (constant what every you want to give like: - 20/30 as per your need)
    
        return cell
    }
    

    【讨论】:

    • 不,不要这样做。表格视图的rowHeight 属性只能设置一次,而不是每次请求单元格时。
    • 但我在我的应用程序中使用它,它运行良好,这就是我回答的原因
    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 2015-12-29
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    相关资源
    最近更新 更多