快速,自动调整自定义表格视图单元格的大小 [英] Swift, Auto Resize Custom Table View Cells

查看:115
本文介绍了快速,自动调整自定义表格视图单元格的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我有一个表格视图,每个单元格中都有一个图像,标签和文本视图.我希望能够根据文本视图中的内容量自动调整单元格的大小. (文本视图是最下面的文本.)

In my app, I have a table view with an image, label and text view in each cell. I would like to be able to auto-resize the cells depending on the amount of content in the text view. (The text view is the lower most text.)

到目前为止,我已经添加了正确的约束.在文本视图的前导,尾随,顶部和底部,并且已禁用滚动和编辑.

So far, I have added the correct constraints; leading, trailing, top and bottom to the text view and have disabled scrolling and editing.

在我的tableViewController.swift文件中,我编写了以下代码:

In my tableViewController.swift file, I have written this code:

override func viewWillAppear(_ animated: Bool) { tableView.estimatedRowHeight = 100 tableView.rowHeight = UITableViewAutomaticDimension }

override func viewWillAppear(_ animated: Bool) { tableView.estimatedRowHeight = 100 tableView.rowHeight = UITableViewAutomaticDimension }

但是,这不起作用,因为当我在文本视图中添加更多文本时,它会被截断.

However, this is not working as when I add more text to the text view, it just cuts off.

也许这与卡片外观有关,我在每个单元格中都有一个UIView充当卡片.

Maybe this has something to do with the card like look, I have got a UIView in each cell acting as a card.

老实说,我不知道我在做什么错

I honestly don't know what I am doing wrong

下面是我的应用程序外观的图片,如果有人可以帮助的话,将不胜感激

A picture is below of what my app looks like and if anyone could help that would be greatly appreciated

推荐答案

检查您的约束是否像这样(基于您的图片):

Check if your constraints are like this(based on your image) :

imageView:设置为容器的顶部和顶部,并具有固定的高度和宽度值.

imageView : set to Top and Leading of your container, with fix height and width values.

标签:您可以将其设置为图像的顶部和水平空间,并同时固定高度和宽度.

label : you can set it to top and horizontal space of your image, with fix height and width as well.

textView:导致图像,顶部空间用于标签,尾随和底部至容器.

textView : leading to image, top space to label, trailing and bottom to container.

并继续使用

tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension

在您的viewWillAppear()

in your viewWillAppear()

这篇关于快速,自动调整自定义表格视图单元格的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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