2017-02-10 36 views
2

也有类似的问题,但没有答案适合我。在动态表格中,我想显示不同高度的图像。每个单元格都有一个UIImageViewcontentMode = .scaleAspectFit,所以图像很好地占据了表格的宽度并根据需要获取高度。带图像的动态UITableView

细胞有4个约束: enter image description here

表视图控制器:

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 
    } 
} 

结果:

enter image description here

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

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

,但它打破了细胞的内容视图的约束。

该项目可以下载here>>

+0

所以基本上你想要的tableview具有动态高度单元? –

+0

正确设置约束条件 – Harish

+0

@Dharmesh Kheni是的,所以单元格的高度等于图像高度。 – sash

回答

3

在你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.swiftcellForRowAt方法是:

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这样:

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将是:

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

感谢您的帮助。不幸的是你的代码打破了约束运行时检查控制台 - 无法同时满足约束条件。 – sash

+0

要修复破坏约束,您需要将约束优先级设置为999.我将在更新它时接受答案。谢谢 – sash

+0

@sash检查更新的代码。 –