Any way to dynamically adjust the height of a UITextView inside a UIStackView inside a UITableViewCell? - ios

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.

Related

Put UITextView underneath undefined number of UITableViewCell's?

I will have a number of cells (typically 5-6), and I want a text view underneath it. But I want the position of the text view to change based on the y-value of the lowest cell.
That way, no matter how many cells, the text view will be directly under the cells.
I tried placing the textview within the tableview, but it hid the cells.
Not sure what else to do.
you could for example set it as the tableFooterView...
(see https://developer.apple.com/reference/uikit/uitableview/1614976-tablefooterview)

Resize UITable Header View Auto Layout

I am building a simple iOS application using Swift.
One of my views, displays a list of comments (cells in a PFQueryTableViewController...I am using Parse.com for my database).
My cells resize happily in order to fit the possible multiple-lines of the comments. This is MAGIC! (if you ask me)
What I am having trouble doing, is making the UIView which is at the top of the TableView resize based on multiple lines worth of content for the Label in that top view (see below).
Instead of doing multiline and resizing the view (I have set up constraints and set lines of text to 0), the label only shows one line, truncating it at the edge of the screen with "..."
Does anyone have any suggestions or solution for how to cause my top UIView to resize based on the content of the UILabel?
I was also thinking this could maybe better be done using a Container View at the top of the TableViewController?
There is a delegate method in UITableViewDelegate that lets you return the height of the head view (heightForHeaderInSection). You have to implement it making sure you return the proper height.
If you have a nib with constraints already, you can load the nib in a #property, and use the method systemLayoutSizeFittingSize that should give you enough height.
For more details checkout this post I wrote a while ago: Customize the UITableview with different height of cells like Facebook feeds in iOS

How to add UIScroller in UITableViewCell?

I have a table with different cells and I need to add a scroller view with special UI in some cells:
I need to develop the UI which you can see in section 0.
So my questions are:
How can I add a scroller view?
How can I load data only for those items which are shown right now?
How can I change the width of "Line" column after scrolling to the right?
I'll answer as best I can with some starting pointers for you...
You need to subclass UITableViewCell and in your subclasses drawRect method you should add a UIScrollView to the contentView.
Not totally sure what you mean here, please expand.
In your new table cell, set it as the delegate of the scroll view you implemented, then you''ll get callbacks when the scrollview scroll with the contentOffset value, which you can use to calculate how much to grow or shrink the line items.

UICollectionView within UITableView. How to tell tableview cell to change height when collection view gets taller?

I have a UITableView and one of my table cells is a UICollectionViewController subclass that contains a UICollectionView of displayed email addresses. When a user adds an email the UICollectionView and it’s cell in the table view should get taller.
I’m currently attempting to do this setting my collectionView height constraint to collectionView.contentSize.height in the LayoutSubviews method of my collection controller/cell class. My issue is that the cell in the UITableView is not changing size when this happens.
I am assuming that this is because there is nothing telling the table view that the height of the email entry cell has changed. I am currently using dynamic cell sizing - or trying to anyway. Even, if I call tableView.reloadData() this still does not work. I’m wondering if someone could give me a high-level idea of how they would set this up.
UPDATE:
I had a broken constraint issue that was part of my problem, but this is still not solved. While have proven that I can update the height constraint and that will update the size of the collection view, it's out of sync. It's always one update behind. Here's an image showing the 'UICollectionView' in green and you can see in the logs that I'm updating the constraint (yes, multiple times) each time after adding a new item to the collection, but it is not updating the bounds of the view instance. In this example, if I were to add a new item to the collection, the next time I inspect bounds.height it would be at 149.5. What am I missing!?
i had answered this question in another tableview inside tableview cell that will work for collection view too ,i explained it in this thread
ask me if you faced any problem
It seems you want to set the height of your table view cell dynamically. This detailed tutorial walks you through making a custom cell class that will have dynamic height. Unfortunately, this tutorial only walks you through how to do this with auto layout constraints and a storyboard. In the tutorial you'll notice that the height of the cell depends on the constraints put on a title label within the cell. Try doing the same thing, but applying the constraints on the content in your cell (the collection view cell).
http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout

UITextView size not changing size after text has wrapped until second character is typed

I have spent at least 10 hours trying to figure this out and I am stuck. I am trying to have UITextView within a UITableViewCell that automatically expands to the size of the textview when the number of lines in the text view has changed. The table cell is inside a static table view. I have looked at almost every question about this on stackoverflow and nothing seems to be working.
My test project is here.
I have created a custom class that automatically resizes the textview. It works perfectly when not in a table cell. You can see this in the example project. There are two instances of the textview; one in a regular view where the textview resizes correctly, and another in a tableview. In the table cell, it doesn't resize until the second character on the next line has been typed. It is odd, because when I log the height of the textview it says that the value of frame.size.height has changed but the text view does not change size.
One thing that worked was creating a custom table cell and then overriding the layoutSubviews method but the problem with that was the bottom border line on the table cell lagged and created an ugly line across the screen. I looked for a solution but could not figure out how to prevent this.
Thank you for taking the time to read this.
I have tried your project and make it work by :
In Tbl View Controller in storyboard, remove AutoLayout. Although you don't set any constraint for the text view, at run time some constraints will be set automatically, and that will clash when the text view tries to change its frame.
In storyboard, set the autoresizing mask of the text view to default (Fix top, Fix Left, flexible right margin, flexible bottom margin).
It will work correctly now.

Resources