如何用图像调整表格视图单元格的大小?

问题描述 投票:0回答:1

我正在尝试制作食谱搜索应用。我正在尝试从API获取结果以及与这些结果关联的图像,并使用基本的表格视图单元格在表格视图中显示它们。

当我下载图像时,图像框架被设置为其内在的大小,因此它将是下载图像的尺寸。当我对此图像设置约束时,它变小了,但您仍然可以看到该框架正在影响文本的放置。

这里是设置约束之前的样子:https://imgur.com/srmKJtV

这里是设置约束后的样子:https://imgur.com/DFLviBK

这是我用来设置单元格的代码:

   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "recipe", for: indexPath)

     cell.imageView?.translatesAutoresizingMaskIntoConstraints = false
     cell.imageView?.heightAnchor.constraint(equalToConstant: 100).isActive = true
     cell.imageView?.widthAnchor.constraint(equalToConstant: 100).isActive = true
     cell.imageView?.image = recipeResults[indexPath.row].image

     cell.textLabel?.numberOfLines = 0
     cell.textLabel?.text = "\(recipeResults[indexPath.row].title)"

    return cell
}

有人知道我如何将图像保持为100x100,并使每一行中的文本排列整齐吗?

[我还注意到我在使单元出队时遇到问题,因为当我单击该行或向上/向下滚动时,图像然后又恢复为正常的大尺寸而没有缩小。我该如何解决?

ios swift image tableview
1个回答
0
投票
[尝试将其设置为在单元格内垂直居中,并在标签上添加约束。我认为您更容易在情节提要中进行设置。
© www.soinside.com 2019 - 2024. All rights reserved.