简体   繁体   English

有什么方法可以动态调整UITableViewCell内UIStackView内UITextView的高度?

[英]Any way to dynamically adjust the height of a UITextView inside a UIStackView inside a UITableViewCell?

My situation: I have a UITextView inside of a UIStackView in a UITableView prototype cell. 我的情况:我在UITableView原型单元的UIStackView内有一个UITextView。

After some research and coding, I've implemented the proper code for dynamic table row height adjustment by returning UITableViewAutomaticDimension in the heightForRowAt and estimatedHeightForRowAt delegate methods. 经过一些研究和编码,我通过在heightForRowAtestimatedHeightForRowAt委托方法中返回UITableViewAutomaticDimension ,实现了用于动态表行高度调整的适当代码。

This works, but the problem is that this "dynamic" adjustment isn't dynamic enough. 这行得通,但是问题是这种“动态”调整不够动态。 It only adjusts the height of the row to match the height of the UITextView after the UITextView's content is done being edited. 仅在编辑完UITextView的内容之后,才调整行的高度以匹配UITextView的高度。

So my goal: to have this same adjustment occur whenever the text view expands dynamically to make all text in it visible, which it already does, yet the table cell doesn't expand with it while editing is still going on (at least I think that's what's happening, but I can't see the second line in the text view because the cell doesn't expand until editing is done). 所以我的目标是:只要文本视图动态扩展以使其中的所有文本都可见,便会进行相同的调整,这已经做到了,但是在编辑仍在进行时,表格单元格不会随之扩展(至少我认为这就是正在发生的事情,但是我无法在文本视图中看到第二行,因为在完成编辑之前,单元格不会展开)。

The intuitive thing to do seems to be to use the textViewDidChange delegate method and to somehow equalize the row's height with the text view's height when text entry or removal results in the text view's height changing... 直观的操作似乎是使用textViewDidChange委托方法,并在文本输入或删除导致文本视图的高度改变时以某种方式使行的高度与文本视图的高度相等...

Yet I can't figure out what to put in there. 但是我不知道该放什么。 I tried adjusting the .rowHeight and .estimatedRowHeight for my table view, but that doesn't seem to do anything while editing, neither with static height values nor with UITableViewAutomaticDimension . 我尝试为表格视图调整.rowHeight.estimatedRowHeight ,但是在编辑时似乎没有执行任何操作,既没有使用静态高度值也没有使用UITableViewAutomaticDimension

Caveat: unlike most other questions about similar issues here on StackOverflow, in my case, the UITextView is inside a UIStackView, which means the stack view controls the size of the text view, and the table row in turn controls the size of the stack view via constraints, meanwhile, I've already gotten the text view to automatically resize to fit its contents, but the table row isn't resizing to match the text view's height while editing is still happening (at least that's the assumption, since I can't visually tell if the text view is expanding while editing is being done, or if it's just wrapping the text to a new line without actually adjusting its frame until editing is complete). 注意:与其他大多数关于StackOverflow上类似问题的问题不同,在我的情况下,UITextView位于UIStackView内部,这意味着堆栈视图控制文本视图的大小,而表行又控制堆栈视图的大小同时,通过约束,我已经使文本视图自动调整大小以适合其内容,但是在编辑仍在进行时,表格行的大小并未调整以匹配文本视图的高度(至少这是假设,因为我可以不能在视觉上分辨完成编辑时文本视图是否在扩展,或者它只是将文本包装到新行而无需实际调整其框架,直到编辑完成。

In short: table rows are only resizing to fit the text view/stack view inside them after text view editing is done because neither the table view's delegate methods nor manually setting .rowHeight and .estimatedRowHeight have any effect while a text view is being edited. 简而言之:完成文本视图编辑后,表行仅会调整大小以适合文本视图/堆栈视图,因为在编辑文本视图时,表视图的委托方法或手动设置.rowHeight.estimatedRowHeight均无效。

Any advice on how to get around this and get the table row and/or text view to resize while editing is happening would be much appreciated. 在编辑过程中如何解决此问题以及如何调整表格行和/或文本视图的大小的任何建议将不胜感激。

Turns out that just writing out that question in detail and then looking at a different question on StackOverflow allowed me to find one of several answers that described the solution to this predicament. 事实证明,只需详细写出该问题,然后在StackOverflow上查看另一个问题,就可以找到描述该困境解决方案的几个答案之一。

The answer: 答案:

func textViewDidChange(_ textView: UITextView) {
    self.listTableView.beginUpdates()
    self.listTableView.endUpdates()
}

Calling .beginUpdates() and .endUpdates() apparently calls the table view's delegate methods without breaking the editing process like .reloadData() does, allowing for truly dynamic table row height changes while editing a text view inside the table row. 调用.beginUpdates().endUpdates()显然会调用表视图的委托方法,而不会像.reloadData()那样破坏编辑过程,从而允许在编辑表行内的文本视图时真正地动态改变表行的高度。

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

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