简体   繁体   English

Swift UITableView 无法正确计算内容高度

[英]Swift UITableView can't calculate content height properly

I'm having this weird issue with UITableView that can't calculate it's content's height properly.我在 UITableView 上遇到了这个奇怪的问题,无法正确计算其内容的高度。

I have custom UITableView class that is embedded in another custom UITableView, I want it to auto-adjust it's height to fit content so I have already:我有一个嵌入在另一个自定义 UITableView 中的自定义 UITableView 类,我希望它自动调整它的高度以适应内容,所以我已经:

override var contentSize: CGSize {
    didSet {
        self.invalidateIntrinsicContentSize()
    }
}

override var intrinsicContentSize: CGSize {
    self.layoutIfNeeded()
    return self.contentSize
}

And now when I use:现在当我使用:

self.estimatedRowHeight = UITableView.automaticDimension // non-zero value like 40 isn't working either
self.rowHeight = UITableView.automaticDimension

the output is the frame that is not full height, when I turn "Scrolling enabled" in this TableView it's scrollable with full content (don't want that):输出是不是全高的框架,当我在这个 TableView 中打开“启用滚动”时,它可以滚动完整的内容(不想要): EstimatedRowHeight 设置为 .automaticDimension

Now when I change现在当我改变

self.estimatedRowHeight = UITableView.automaticDimension

to:到:

self.estimatedRowHeight = 0

the output is exactly what I would want to have except the content text is cut...输出正是我想要的,除了内容文本被剪切...... 估计行高度 = 0

Here's my CommentCell:这是我的评论单元: 评论单元格

Console isn't showing any errors with autolayout in any case.在任何情况下,控制台都不会显示自动布局的任何错误。

Do you maybe know what's going on?你知道发生了什么吗? I have spent literally days trying to get those comments to work and that's the last thing I need.我真的花了几天时间试图让这些评论生效,这是我最不需要的。 If you need any more info please just tell me.如果您需要更多信息,请告诉我。

Edit: If i change estimatedRowHeight to a large number for example 500 I get loads of empty space under cells:编辑:如果我将估计行高度更改为一个大数字,例如 500,我会在单元格下获得大量空白空间: 在此处输入图片说明 So it looks like TableView can't fix the cell height to content.所以看起来 TableView 无法将单元格高度固定为内容。 Maybe this will help someone.也许这会帮助某人。

Maybe it's about the textfield inside the CellView.也许是关于 CellView 中的文本字段。 Did you set it's Layout to wraps?您是否将其 Layout 设置为 wraps?

Also I would try to set it's intrinsic size value to 'placeholder' inside the Size Inspector.此外,我会尝试将其内在大小值设置为“大小检查器”中的“占位符”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM