TextView & 的动态单元格大小同一单元格中的 ImageView [英] Dynamic cell Size for TextView & ImageView in same cell

查看:21
本文介绍了TextView & 的动态单元格大小同一单元格中的 ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ImageView 和一个 TextView,我需要动态单元格大小,这取决于 textView 里面是否有更多文本.

I have an ImageView and a TextView and I need Dynamic cell size for both depending if the textView has more text inside of it.

我正在使用 UITableViewAutomaticDimension

界面示例:

cell 会随着文本数量而扩展,但不会扩展 UIImage.我究竟做错了什么?我应该如何添加约束以适应这种情况?如果您需要更多信息,请告诉我.

The cell is expanding with the text amount but is not expanding the UIImage. What am I doing wrong? How should I add the constraints to adapt to this situation? Let me know if you need more information.

图像低于 storyBoard 中的固定单元格大小.

The image is going beneath the fixed cell size in the storyBoard.

如果我在第二张图片中的红色圆圈处添加底部约束,则 textView 文本仅在一行上并且不再是动态的.

If I add a bottom constraint where the red circle is in the second image the textView text is only on one line and is no longer dynamic.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {


        return UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

        return UITableViewAutomaticDimension
    }

如果我将底部添加到 superView textView 不再是动态的.(对比上图.)

if I add bottom to superView textView no longer dynamic. (Compare to image above.)

底部约束是我试图让它们协同工作时的主要问题.

The bottom constraint is my main issue when trying to make them work together.

推荐答案

如果您使用自动布局,则有一种更简单的方法可以做到这一点,无需任何计算,因此您也可以忘记 heightForRowAtIndexPath代码>委托,让自动布局完成它的工作.

If you’re using Auto layout, there is a simpler way to do this without any calculating stuff, so you can also forget away heightForRowAtIndexPath delegate, just let Auto layout do its job.

嗯,这个技巧的本质只有三件事:

Well, the essential of the trick are just three things:

在单元格内容上设置所需的自动布局参数.

Set needed auto layout params on cell content.

  1. 在 tableview 单元格高度上启用 UITableViewAutomaticDimension.

  1. Enable UITableViewAutomaticDimension on tableview cell height.

为 tableview 提供一些预估数字(用于优化滚动速度).

Give some pre-estimation number for tableview (for optimizing scrolling speed).

在单元格内容上设置所需的自动布局参数

Set needed auto layout params on cell content

请记住规则是表格视图单元格上的所有项目都需要固定在父视图的顶部、底部、前导和尾随边缘.例如,如果您的表格视图单元格有两个标签: brandNameLabel &品牌地址标签,所以这些标签的布局是:

Just remember the rule is all items on table view cell need to be pinned on top, bottom,leadingand trailing edges from parent view. For example if your table view cell have two labels: brandNameLabel & brandAddressLabel, so the layout for those label would be:

对于brandNameLabel:

对于品牌地址标签:

有关更多信息,您应该查看此参考

For more you should see this REFERENCE

完成自动布局后,在 viewdidLoad() 方法中添加这两行代码

Once you are done with the auto-layout, in your viewdidLoad() method add these two lines of code

tableView.estimatedRowHeight = 100//根据你的数据给出一个估计的大小tblAmount.rowHeight = UITableViewAutomaticDimension

你就完成了.现在,单元格将自动调整为您单元格的内容.

and you are done. Now the cells will automatically adjust to the content of your cell.

这篇关于TextView & 的动态单元格大小同一单元格中的 ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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