I have been searching the internet for hours but can't seem to find a solution in Xamarin.iOS where the table row height automatically increases when user types in textview.
I have a UITextView (that grows when user types), inside a UITableViewCell.
Any ideas?
Related
Assume we have UITableView holding a large number of custom UITableViewCells which contain UITextFields and/or UITextViews.
We randomly tab on any cell an the text control inside becomes the FirstResponder. The keyboard appears and we can type some text. Everything is fine.
Is there any reliable information on what happens with the TextField when the cell is re-used for another cell?
In tableView.cellForRowAt:indexPath the cells are dequeued using dequeueReusableCell. Is it my responsibility to keep track on which cell contained the FirstResponder and resign / reset it when the cell is scrolled out of view / back into view as I do for my custom view model?
I ran different test and it seems that this is handled automatically somehow. However, I would like to be sure. Could be coincidence that I did not ran into any problems in my tests.
I am using a UITableViewController with static cells, as a user details form. Everything works, until I tap in one of the cells's text field, the keyboard (or custom inputView) shows up, and you scroll the table down. Cells are then not shown, instead a blank space is shown. Scrolling up and back down then correctly shows them.
If I scroll the tableview down until the "geboortedatum" field shows up,
and press on that textfield, a custom inputView with a date picker shows up:
Now, if I scroll the tableview down, the next rows show up blank:
Two rows are supposed to show up there, like this:
It's not related to the inputView with the date picker, the same happens when the plain old keyboard shows up and you scroll the table down.
I have not overridden any of the tableview delegate or datasource methods apart from showing custom headers (but the problem is the same when I remove that), and am not using UITableViewCell subclasses, just static cells in a storyboard. It seems like an iOS bug to me, but is there any way to work around this?
I think I found the cause and the solution. The problem is that these last 2 cells don't actually fit in the storyboard design:
They are there of course, just not visible by default. If I change the storyboard to freeform with a custom height to make it all fit, like this:
Then the problem doesn't show up anymore. The cells are not hidden anymore on scroll, as before. Very strange.
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:
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.
I've created a simple project. It contains a UITableViewController with self-sizing prototype cells that contain a UITextField and a UILabel for displaying validation message. The controls are grouped in a UIStackView, but that doesn't seem to matter.
The label is only visible if there is a message to display, otherwise it's size collapses to 0. To adjust the size of the cell I simple call the recommended:
tableView.beginUpdates()
tableView.endUpdates()
If I scroll the view with the UITextfield remaining first responder (ie w/o dismissing the keyboard), so that the cell is no longer visible, when scrolling back to the cell, it is misaligned.
I've hosted the sample project at https://github.com/ionel71089/tableViewTextField
Note:
Easiest way to reproduce is on Simulator iPhone 6 9.2, by editing let's say the 50th cell, scrolling to the top and then back to the cell.
How would I be able to horizontally scroll a UITextField in a UITableViewCell if the text overflows? I want it so that the user can scroll the text to see all of it, but it should not interfere with the swipe left to delete gesture in a UITableView. Is there a way to do this?
This should be happening automatically, but the hit regions are very small. This will likely lead to user confusion since two gestures in very small areas do two different things.
Are you sure that's a UITextField you're using in the cell? Looks like it's a UILabel because of the truncation.