<i id='279gK'><tr id='279gK'><dt id='279gK'><q id='279gK'><span id='279gK'><b id='279gK'><form id='279gK'><ins id='279gK'></ins><ul id='279gK'></ul><sub id='279gK'></sub></form><legend id='279gK'></legend><bdo id='279gK'><pre id='279gK'><center id='279gK'></center></pre></bdo></b><th id='279gK'></th></span></q></dt></tr></i><div id='279gK'><tfoot id='279gK'></tfoot><dl id='279gK'><fieldset id='279gK'></fieldset></dl></div>
    <tfoot id='279gK'></tfoot>
    <legend id='279gK'><style id='279gK'><dir id='279gK'><q id='279gK'></q></dir></style></legend>
    • <bdo id='279gK'></bdo><ul id='279gK'></ul>
      1. <small id='279gK'></small><noframes id='279gK'>

        带图像的动态 UITableView

        Dynamic UITableView with images(带图像的动态 UITableView)

          <bdo id='gLyi3'></bdo><ul id='gLyi3'></ul>

            <tfoot id='gLyi3'></tfoot>

            <small id='gLyi3'></small><noframes id='gLyi3'>

            <i id='gLyi3'><tr id='gLyi3'><dt id='gLyi3'><q id='gLyi3'><span id='gLyi3'><b id='gLyi3'><form id='gLyi3'><ins id='gLyi3'></ins><ul id='gLyi3'></ul><sub id='gLyi3'></sub></form><legend id='gLyi3'></legend><bdo id='gLyi3'><pre id='gLyi3'><center id='gLyi3'></center></pre></bdo></b><th id='gLyi3'></th></span></q></dt></tr></i><div id='gLyi3'><tfoot id='gLyi3'></tfoot><dl id='gLyi3'><fieldset id='gLyi3'></fieldset></dl></div>
                <tbody id='gLyi3'></tbody>

              1. <legend id='gLyi3'><style id='gLyi3'><dir id='gLyi3'><q id='gLyi3'></q></dir></style></legend>
                • 本文介绍了带图像的动态 UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有类似的问题,但没有一个答案对我有用.在动态表中,我想显示具有不同高度的图像.每个单元格都有一个 UIImageViewcontentMode = .scaleAspectFit,因此图像很好地采用了表格的宽度并根据需要采用了高度.

                  There are similar questions, but non of the answers worked for me. In a dynamic table I want to display images that have different heigh. Each cell has a UIImageView with contentMode = .scaleAspectFit so the image nicely takes the width of the table and takes the height as much as needed.

                  Cell 有 4 个约束:

                  Cell has 4 constraints:

                  表格视图控制器:

                  class TableTableViewController: UITableViewController {
                  
                      override func viewDidLoad() {
                          super.viewDidLoad()
                  
                          tableView.estimatedRowHeight = 100
                          tableView.rowHeight = UITableViewAutomaticDimension
                      }
                  
                      override func numberOfSections(in tableView: UITableView) -> Int {
                  
                          return 1
                      }
                  
                      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                  
                          return 2
                      }
                  
                      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                          let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ImageTableViewCell.self), for: indexPath) as! ImageTableViewCell
                  
                          let image = indexPath.row == 0 ? UIImage(named: "1.jpg")! : UIImage(named: "2.jpg")!
                          cell.customImageView.image = image
                  
                          return cell
                      }
                  }
                  

                  结果:

                  您可以看到单元格的高度不正确(图像视图顶部和底部的图像视图的红色背景).我相信这是因为图像视图的 intrinsicContentSize 等于图像大小,这就是单元格高度计算不正确的原因(不考虑内容模式).我尝试计算图像的高度并为图像视图添加高度约束:

                  As you can see that the height of the cell is incorrect (red background of the image view on top and bottom of the image view). I believe this happens because intrinsicContentSize of the image view is equal to the image size and thats why the height of the cell is calculated incorrectly (content mode is not taken into account). I tried calculating height of the image and adding height constraint for the image view:

                  cell.heightConstraint.constant = cell.frame.width * image.size.height /  image.size.width
                  

                  但它打破了单元格的内容视图约束.

                  but it breaks cell's content view constraints.

                  项目可以在这里下载>>

                  推荐答案

                  在你的 ImageTableViewCell.swift

                  import UIKit
                  
                  class ImageTableViewCell: UITableViewCell {
                  
                      @IBOutlet weak var customImageView: UIImageView!
                  
                      internal var aspectConstraint : NSLayoutConstraint? {
                          didSet {
                              if oldValue != nil {
                                  customImageView.removeConstraint(oldValue!)
                              }
                              if aspectConstraint != nil {
                                  customImageView.addConstraint(aspectConstraint!)
                              }
                          }
                      }
                  
                      override func prepareForReuse() {
                          super.prepareForReuse()
                          aspectConstraint = nil
                      }
                  
                      func setPostedImage(image : UIImage) {
                  
                          let aspect = image.size.width / image.size.height
                  
                          aspectConstraint = NSLayoutConstraint(item: customImageView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: customImageView, attribute: NSLayoutAttribute.height, multiplier: aspect, constant: 0.0)
                  
                          customImageView.image = image
                      }
                  }
                  

                  在您的 TableTableViewController.swift 中,您的 cellForRowAt 方法将是:

                  And into your TableTableViewController.swift your cellForRowAt method will be:

                  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                      let cell = tableView.dequeueReusableCell(withIdentifier: "ImageTableViewCell", for: indexPath) as! ImageTableViewCell
                  
                      let image = imageArr[indexPath.row]
                      cell.setPostedImage(image: image!)
                      return cell
                  }
                  

                  并以这种方式声明您的 imageArr:

                  And declare your imageArr this way:

                  let imageArr = [UIImage(named: "1.jpg"), UIImage(named: "2.jpg")]
                  

                  您的竞争代码将是:

                  import UIKit
                  
                  class TableTableViewController: UITableViewController {
                  
                      let imageArr = [UIImage(named: "1.jpg"), UIImage(named: "2.jpg")]
                      override func viewDidLoad() {
                          super.viewDidLoad()
                  
                          tableView.estimatedRowHeight = 100
                          tableView.rowHeight = UITableViewAutomaticDimension
                      }
                  
                      override func numberOfSections(in tableView: UITableView) -> Int {
                  
                          return 1
                      }
                  
                      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                  
                          return 2
                      }
                  
                      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                          let cell = tableView.dequeueReusableCell(withIdentifier: "ImageTableViewCell", for: indexPath) as! ImageTableViewCell
                  
                          let image = imageArr[indexPath.row]
                          cell.setPostedImage(image: image!)
                          return cell
                      }
                  }
                  

                  THIS 将是您的结果.

                  要修复约束问题,将 aspectConstraint 优先级设置为 999aspectConstraint 将是:

                  To fix constraint issue set aspectConstraint priority to 999 and aspectConstraint will be:

                  internal var aspectConstraint : NSLayoutConstraint? {
                      didSet {
                          if oldValue != nil {
                              customImageView.removeConstraint(oldValue!)
                          }
                          if aspectConstraint != nil {
                              aspectConstraint?.priority = 999  //add this
                              customImageView.addConstraint(aspectConstraint!)
                          }
                      }
                  }
                  

                  这篇关于带图像的动态 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Auto layout constraints issue on iOS7 in UITableViewCell(UITableViewCell中iOS7上的自动布局约束问题)
                  How to resize superview to fit all subviews with autolayout?(如何调整超级视图的大小以适应所有具有自动布局的子视图?)
                  reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling(具有动态单元格高度的 UITableView 的 reloadData() 导致跳跃滚动)
                  What is NSLayoutConstraint quot;UIView-Encapsulated-Layout-Heightquot; and how should I go about forcing it to recalculate cleanly?(什么是 NSLayoutConstraint“UIView-Encapsulated-Layout-Height?我应该如何强制它干净地重新计算?) - IT屋-程序员
                  quot;Auto Layout still required after executing -layoutSubviewsquot; with UITableViewCell subclass(“执行 -layoutSubviews 后仍需要自动布局带有 UITableViewCell 子类)
                  Flip UIImageViews for Right to Left Languages(从右到左语言翻转 UIImageViews)

                    <tbody id='I8sGO'></tbody>

                      <bdo id='I8sGO'></bdo><ul id='I8sGO'></ul>
                      <legend id='I8sGO'><style id='I8sGO'><dir id='I8sGO'><q id='I8sGO'></q></dir></style></legend>

                        <i id='I8sGO'><tr id='I8sGO'><dt id='I8sGO'><q id='I8sGO'><span id='I8sGO'><b id='I8sGO'><form id='I8sGO'><ins id='I8sGO'></ins><ul id='I8sGO'></ul><sub id='I8sGO'></sub></form><legend id='I8sGO'></legend><bdo id='I8sGO'><pre id='I8sGO'><center id='I8sGO'></center></pre></bdo></b><th id='I8sGO'></th></span></q></dt></tr></i><div id='I8sGO'><tfoot id='I8sGO'></tfoot><dl id='I8sGO'><fieldset id='I8sGO'></fieldset></dl></div>

                          <tfoot id='I8sGO'></tfoot>

                          <small id='I8sGO'></small><noframes id='I8sGO'>