如何使UITableViewCell调整高度以适应其内容? [英] How to make UITableViewCell adjust height to fit its content?

查看:71
本文介绍了如何使UITableViewCell调整高度以适应其内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个 UITableViewCell ,如下所示,它将根据其内容视图调整其高度。中心的图像应与单元格框架一样宽,并按比例调整其高度。

I'm trying to make a UITableViewCell like below, which will adjust its height according to its content view. The image in the center should be as wide as the cell frame, and adjust its height proportionally.

如您所见,单元格中的图像不会像我想要的那样显示。我试图在覆盖func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - >中将 UIImageView 的高度设置为320.0。 UITableViewCell ,但根本没有帮助。

As you can see, the image in the cell doesn't display like the way I want. I tried to set the height of the UIImageView to 320.0 in override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell, but doesn't help at all.

问题:如何让单元格尊重图像的高度?

Question: how can I make the cell to respect the height of image?

FYI:


  1. 我没有覆盖 func tableView( tableView:UITableView,heightForRowAtIndexPath indexPath:NSIndexPath) - > CGFloat

  2. 我没有设置高度约束到 UIImageView 或其他什么,我正在使用paddings仅用于安排视图。

  3. 使用 aspect-fit 模式显示图像。

  1. I'm not overriding func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
  2. I don't set a height constraint to the UIImageView or anything, I'm using paddings only to arrange views.
  3. The image is display using aspect-fit mode.

编辑:

我进一步向图片添加了高度限制:

I moved further to add a height constraint to picture:

class MyTableViewPostCell: UITableViewCell {
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var pictureImageView: UIImageView!
    @IBOutlet weak var pictureImageViewHeightConstraint: NSLayoutConstraint!
}

class MyTableViewController: UITableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("MyTableViewPostCell") as MyTableViewPostCell
        cell.contentLabel.text = "This is a very very very very very very very long content."
        cell.pictureImageViewHeightConstraint.constant = 320
        cell.pictureImageView.sd_setImageWithURL(NSURL(string: "http://somedomain.com/pic"))
        return cell
    }
}

运行时出现约束冲突错误。见下文:

And I got a constraint conflict error when running. See below:

(
    "<NSLayoutConstraint:0x7ff7b5026c30 V:[UIImageView:0x7ff7b50289e0(40)]>",
    "<NSLayoutConstraint:0x7ff7b5029a90 V:[UIImageView:0x7ff7b5029990(320)]>",
    "<NSLayoutConstraint:0x7ff7b502a630 UIImageView:0x7ff7b50289e0.top == UITableViewCellContentView:0x7ff7b5028910.topMargin + 6>",
    "<NSLayoutConstraint:0x7ff7b502a770 V:[UIImageView:0x7ff7b50289e0]-(12)-[UILabel:0x7ff7b5029390'This is a very very very ...']>",
    "<NSLayoutConstraint:0x7ff7b502a950 V:[UILabel:0x7ff7b5029390'This is a very very very ...']-(10)-[UIImageView:0x7ff7b5029990]>",
    "<NSLayoutConstraint:0x7ff7b502aa40 UITableViewCellContentView:0x7ff7b5028910.bottomMargin == UILabel:0x7ff7b5029c20'Label'.bottom + 10>",
    "<NSLayoutConstraint:0x7ff7b502aa90 V:[UIImageView:0x7ff7b5029990]-(17)-[UILabel:0x7ff7b5029c20'Label']>",
    "<NSLayoutConstraint:0x7ff7b2cae430 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ff7b5028910(283.5)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7ff7b5029a90 V:[UIImageView:0x7ff7b5029990(320)]>

我不知道在哪里 UIView-Encapsulated-Layout-Height 来自,但似乎是导致冲突。有任何解决这个问题的方法吗?谢谢!

I don't know where UIView-Encapsulated-Layout-Height comes from, but seems like it is causing the conflict. Any way to fix this? Thanks!

推荐答案

适用于iOS 7及以下版本

使用AutoLayout并正确添加约束,然后使用 UITableViewCellHeightForRow 方法获得动态高度。

Use AutoLayout and add constraints properly, then use the UITableViewCellHeightForRow method for dynamic height.

对于iOS 8及以上版本

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

这篇关于如何使UITableViewCell调整高度以适应其内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆