加载图像后调整UITableViewCell的大小

时间:2017-10-03 09:00:51

标签: ios swift uitableview uitableviewautomaticdimension

我使用动态高度用于tableview单元格和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
}

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

致电

tableView.beginUpdates() tableView.endUpdates()

下载图像并设置高度约束后。希望这有帮助!

修改

为单元格提供正确高度的另一个选项是从服务器接收图像大小。通过这种方式,您可以使用恰当的计算单元格高度来提供表格。

答案 2 :(得分:-2)

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

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
}