My situation: I have a UITextView inside of a UIStackView in a UITableView prototype cell.
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.
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.
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...
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
.
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).
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.
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.
The answer:
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.