简体   繁体   English

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

[英]How to resize table view cell with image?

I'm trying to make a recipe search app.我正在尝试制作一个食谱搜索应用程序。 I am trying to get results from the API and also an image associated with these results and display them in a table view, using the basic table view cell.我正在尝试从 API 以及与这些结果相关联的图像中获取结果,并使用基本表视图单元格将它们显示在表视图中。

When I download the image, the image frame is set to its instrinsic size, so it'll be the dimensions of the download image.当我下载图像时,图像框架设置为其固有大小,因此它将是下载图像的尺寸。 When I set constraints on this image, it's smaller but you can still see the frame is effecting the text placement.当我在此图像上设置约束时,它变小了,但您仍然可以看到框架正在影响文本放置。

Here's what it looks like before constraints are set: https://imgur.com/srmKJtV这是设置约束之前的样子: https://imgur.com/srmKJtV

Here's what it looks like after constraints are set: https://imgur.com/DFLviBK这是设置约束后的样子: https://imgur.com/DFLviBK

This is my code I'm using to set the cell up:这是我用来设置单元格的代码:

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
}

Does anyone know how I can keep the image to be 100x100 and have the text in each row lined up nicely?有谁知道我如何将图像保持为 100x100 并使每一行中的文本排列得很好?

I also notice I have an issue with dequeue the cell, as when I click on the row or scroll up/down, the image then turns back to it's normal big size without keeping it small.我还注意到我在使单元格出列时遇到问题,因为当我单击该行或向上/向下滚动时,图像会变回正常的大尺寸而不保持小尺寸。 How can I fix this?我怎样才能解决这个问题?

Setting the width and height constraints for your UIImage is not enough.UIImage设置宽度和高度限制是不够的。

Try setting it to center vertical within the cell, and add constraint with your label too.尝试将其设置为在单元格内垂直居中,并使用您的 label 添加约束。 I think it's easier for you to set it in storyboard.我认为在 storyboard 中设置它更容易。

Use custom cell instead of basic cell.使用自定义单元格而不是basic单元格。

In this way you'll be able to customize what component you want.通过这种方式,您将能够自定义您想要的组件。

  • Create your own custom cell via .xib or in Interface Builder通过.xib或在Interface Builder中创建您自己的自定义单元格

  • Add horizontal UIStackView into your cell and constraint it's leading , trailing , top and bottom .将水平UIStackView添加到您的单元格中并约束它的leadingtrailingtopbottom

  • Add an imageView and label to the stackView .imageViewlabel添加到stackView You should give a width constraint to your imageView .你应该给你的imageView一个width限制。 In your case it is hard-coded 100 .在您的情况下,它是硬编码的100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM