xcode 快速、自动调整自定义表格视图单元格的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44766814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift, Auto Resize Custom Table View Cells
提问by Luke Roberts
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.
到目前为止,我已经添加了正确的约束;文本视图的前导、尾随、顶部和底部,并已禁用滚动和编辑。
In my tableViewController.swift file, I have written this code:
在我的 tableViewController.swift 文件中,我编写了以下代码:
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.
但是,这不起作用,因为当我向文本视图添加更多文本时,它只是切断了。
Maybe this has something to do with the card like look, I have got a UIView in each cell acting as a card.
也许这与卡片的外观有关,我在每个单元格中都有一个 UIView 作为卡片。
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
下面是我的应用程序外观的图片,如果有人可以提供帮助,将不胜感激
回答by Gustavo de Paiva Caiafa
Check if your constraints are like this(based on your image) :
检查您的约束是否是这样的(基于您的图像):
imageView : set to Top and Leading of your container, with fix height and width values.
imageView :设置为容器的顶部和前导,具有固定的高度和宽度值。
label : you can set it to top and horizontal space of your image, with fix height and width as well.
label :您可以将其设置为图像的顶部和水平空间,也可以固定高度和宽度。
textView : leading to image, top space to label, trailing and bottom to container.
textView :导致图像,顶部空间标签,尾随和底部到容器。
And keep using
并继续使用
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
in your viewWillAppear()
在你的 viewWillAppear()
回答by Zack
Update for swift 4.2
Swift 4.2 的更新
Use:
用:
UITableView.automaticDimension
Instead of:
代替:
UITableViewAutomaticDimension
回答by Abadii176
Make sure that the content mode is set to Scale To Fill
of your UITextView
Make sure that there are no height constraints for the UITextView
and the card UIView
确保将内容模式设置为Scale To Fill
您的UITextView
确保UITextView
和卡没有高度限制UIView
Try to add the estimated height into viewDidLoad
:
尝试将估计的高度添加到viewDidLoad
:
override func viewDidLoad() {
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
}
Maybe the AutoHeight is not working because of the UIView
above the UITextView
. Try to call the sizeToFit
and layoutIfNeeded
methods for the UIView
in the cellForRowAt
:
可能由于UIView
上述原因,AutoHeight 无法正常工作UITextView
。尝试调用sizeToFit
和layoutIfNeeded
方法为UIView
在cellForRowAt
:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as! CustomTableViewCell
cell.vwCard.sizeToFit()
cell.vwCard.layoutIfNeeded()
return cell
}
You can also try the sizeToFit
and layoutIfNeeded
as well for the UITextView
.
您也可以尝试sizeToFit
和layoutIfNeeded
以及UITextView
。
Hope this works........
希望这有效......
回答by Manish Methani
Set bottom of your textView with bottom of that white UIView and make sure that white UIView has left,right,top and bottom constraints :)
将 textView 的底部设置为白色 UIView 的底部,并确保白色 UIView 具有左、右、顶部和底部约束:)