快速、自动调整自定义表格视图单元格的大小 [英] Swift, Auto Resize Custom Table View Cells
问题描述
在我的应用程序中,我有一个表格视图,每个单元格中有一个图像、标签和文本视图.我希望能够根据文本视图中的内容量自动调整单元格的大小.(文本视图是最下方的文本.)
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 = 100tableView.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 : 您可以将其设置为图像的顶部和水平空间,也可以固定高度和宽度.
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
在您的视图中WillAppear()
in your viewWillAppear()
这篇关于快速、自动调整自定义表格视图单元格的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!