动态设置tableview单元格行高? [英] Set tableview cell row height dynamically?

查看:28
本文介绍了动态设置tableview单元格行高?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有标签和图像的表格视图.在某些单元格中,没有图像,因为我使用 imageView.removeFromSuperview() 将其从单元格中删除.当单元格中有图像时,行高为 445,并勾选自定义".

I have a tableview with a label and an image. in some cells, there is no image as i used imageView.removeFromSuperview() to remove it from the cell. When there is an image in the cell, the row height is 445 and "Custom" is checked off.

如何根据标签的长度而不是删除图像视图后图像视图的长度/大小动态设置行高?

how can i set the row height dynamically according to how long the label is instead of how long/big the imageview is after i remove the imageview?

推荐答案

如果你想要动态行高,你可以定义你的约束(确保它们是明确的),将标签的 numberOfLines 设置为零,然后在 viewDidLoad 中,告诉它行应该自动调整它们的高度:

If you want dynamic row height, you can define your constraints (making sure they're unambiguous), set the label's numberOfLines to zero, and then in viewDidLoad, tell it that rows should automatically adjust their height:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44

如果您还想隐藏/显示 UIImageView,我必须承认我并不热衷于 removeFromSuperview 方法(因为当单元格被重用时,你必须重新添加图像视图,也可能重建它的约束)有几个选项:

If you want to hide/show a UIImageView as well, I must confess that I'm not crazy about the removeFromSuperview approach (because when the cell is reused, you have to re-add the image view, and possibly rebuilding its constraints, too) there are a few options:

  1. 对于带有图像视图的单元格和另一个没有图像视图的单元格,您可以使用不同的单元格原型.然后 cellForRowAtIndexPath 只需要实例化正确的单元格即可.

  1. You could have a different cell prototype for the cell with the image view and another without the image view. Then cellForRowAtIndexPath just needs to instantiate the right cell.

您可以继续定义一整套约束,这些约束对于图像的存在和没有图像都是明确的.然后,您可以激活约束并根据需要将图像视图设置为hidden:

You could go ahead and define a full set of constraints that are unambiguous for both the presence of the image and without the image. And then, you can activate the constraint and set the image view to hidden as appropriate:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell

    let image = ...  // let's say it was an optional, set if needed, left `nil` if not

    cell.customImageView?.image = image

    if image == nil {
        cell.customImageView.hidden = true
        cell.imageBottomConstraint.active = false

        cell.customLabel.text = ...
    } else {
        cell.customImageView.hidden = false
        cell.imageBottomConstraint.active = true
    }

    return cell
}

当有一组相互竞争的约束来决定单元格的高度时,诀窍是确保它们有不同的优先级和/或它们使用不等式,所以如果这两组都有效,你就不会有不可满足的冲突(例如,图像视图可能具有更高的优先级).

The trick when having competing sets of constraints that dictate the height of the cell is to just make sure that they have different priorities and/or they use inequality so if both sets are in effect, you don't have an unsatisfiable conflict (e.g. the image view might have higher priority).

如果需要,您可以老派"并以编程方式确定标签字段的大小,然后实现 heightForRowAtIndexPath,但自动布局使此过程变得不必要.>

You can, if you want, go "old school" and programmatically determine the size of the label field, and then implement heightForRowAtIndexPath, but auto layout makes this process unnecessary.

这篇关于动态设置tableview单元格行高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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