UITableViewCell dynamic height + hidden elements take up space in Swift? - ios

This is the scenario: I have a Table in which there are Cells with dynamic height. In each cell I have a label and a switch. When I run the App only these two can be seen but then I set the switch to true then a (hidden?) textview appears under these two elements making the height of the cell bigger.
How can I do this? Because when I try hidden elements take up place as well.
This is what I want:
Reality:
How can I do this?
I tried using the new Stack View which can handle it fine, BUT NOT in a table cell....
How can I solve this?

I solved it mixing this answer: Dynamic Height Issue for UITableView Cells (Swift) for the dynamic height and then for the views that show or not I added a height constraint to the view and then on cellForRowAtIndexPath just set the constant of the constraint to 0 if you need to hide it or to your height if you show it.
Hope it helps!

I think it could be solved using constraints:
- set the height constraint for your text view,
- create the outlet for that constraint in your view controller,
- change the constant property of the constraint in your view controller when the switch is used.

If you hide your text view, your text view is still there, text length is as long as it is not hidden.
So instead of hide/show the text of the text view, you'd better to update the text of your text view from "" -> "what ever you want to show."
In this case, the auto layout can handle your query fair easily. You only need the set
self.tableView.estimatedRowHeight = 65 //whatever number
self.tableView.rowHeight = UITableViewAutomaticDimension
and in the cell xib/storyboard, add constraints on thetop andbottom of your text view, don't also forget to make your text view not scrollable as well.
Actually, I recommend you to simply useUILabel in stead ofUITextField, you just need to set thelineNumber of theLabel to be 0, and you can neglect the scroll and size setting of the text view.

Related

ObjC - How to show/hide view that has proportional height constraint?

I have a view that has a proportional height constraint of its superview and the multiplier is 0.09. And there is a UITableView below that view.
When I press a button I want to hide the top view completely and when I tap that button again I want to display it again.
Since it has a proportional height constraint and has a multiplier, I cannot directly change its constant. I need to create a new constraint and assign that constraint to that view. I have tried changing multiplier and assigning it, it worked and hidden the view but when I wanted to display it again and set the multiplier to 0.09 it didn't work.
Do you have any solutions?
Thanks.
first off, you shouldn't code a view like that above the UITableView that will be shrunk and then force the UITableView to resize. Doing it this way is prone to brokenness. The easist way to achieve this is make the view at the top a header of the UITableView. And when the button is pressed, you "realoaddata" on the UITableView which will then call the header view height delegate method where you then change the height based on a boolean value or something. Don't do what you're doing. also, use a collection view instead. good luck

Swift dynamic tableview height

I have a tableView which I want to size dynamically in height depending on how many cells are there (and to pull/push all views underneath it)
I've tried to archieve this by putting it in a StackView, but TableView content doesn't show unless I give it a fixed height constraint, which is opposite of what I'm trying to do.
Also tried this Swift dynamic tableview height based on content with no change, as the comment from OP said too.
Thanks in advance.
Set table frame or table height Constraint outlet with table content size means set the table view height constraint equal to its content size after the table view has done the loading.
tableHeightConstraint.constant = tableView.contentSize.height

Dynamic height of two UITableViews embedded in a single view with Auto Layout

I`m trying to adjust the height of two tableviews embedded in a single viewController. .
This is how it is currently displayed
Current constraints
If possible, I want to adjust the height of each table to avoid the scrolling effect of the table (it is not possible when the total content of the tables is higher than the screen, in that case the height of each table must be the same )
I have manually changed the constraints to show you what behavior I want
Expected behavior 1
Expected behavior 2
I have no idea what constraints I have to modify and what part has to be programmatically
Here is a scenario:
Mapping Data to table.
Get tableview's content size
Set the constraint = height of the content size
Update constraint layout.
In your case, i would like to recommend to use 1 table view with 2 sections.
Hope this help.
The best solution would be not using tableView for the first section as its not a complicated content to be displayed on tableView. Even if you want to use tableView, try this.
Take height constraint of the top tableView. Take outlet of the height constraint in your controller. Then just after you are reloading the tableView, assign the contentSize.height to the constraint and call self.view.layoutIfNeeded(). This will change the height of the tableView to be same as content.
You can do same with bottom tableView.

Swift dynamic table text goes outside cell width

i just created a uitable view with dynamic cells, and as you can see with the insets, the cells have a correct width. But as shown on the image, the label goes out of the screen. I've set truncate tail, fixed font size... I can't manage to get this text truncated.
Should i do it programatically when creating the cell.text.value?
Help is very much appreciated here.
You might not have set frame in LayoutSubviews(). Initially when tableviewcell loads it always gives same width value but when layoutSubviews gets called it gives correct frame.
If you have done it in storyboard then you might have given hardcoded value.
Use Custom TableViewCell's
You can design the way you want and also you can set label/title width height font etc easily .
Make sure you register your tableview cell in viewdidload() or else it will crash
Here are two stack views, each with the same content - the text of the top label is this is a really long label that will extend past the right edge
The top one does not have a Trailing constraint.
The bottom one does have a Trailing constraint of 8.
That really looks like the issue you are running into.
Solved the issue. Initially i had dropped a table view inside the view controller and set the prototype cell to 1 in IBinspector. For an unknown reason, the constraints were not working. Ended up setting protoype cells to 0 and dropping a new table view cell from the object library.
That did the trick.

How to add subviews in Table View Cell while Expanding them and perform actions on their elements of SubViews?

Image Depicting that I have to add this kind of subview to my existing cell and also same subview on multiple clicks of Add icon.
I am facing difficulties in adding subview. If anyone can provide me the correct approach to handle out this structure, help me out.
You can design your cell for a expanded mode in interface builder and set the auto layout constraint correctly instead of adding subview to the tableView cell while cell is expanding.
1) In interface builder design your cell's view for expanded cell and add subviews in a view which you want to be added while cell is expanded.
2) Now set the auto layout constraint correctly and don't forget to set the height constraint for the view which contains all the subviews for your expended cell.
3) Take the outlet of the height constraint, you created in step 2.
4) Now set the height constraint's constant value to 0 for the normal cell and a value > 0 for the expanded cell.
heightConstraint.constant = 100
The easiest way to do this is to treat each addition as a new row and keep track of that, rather than put it all in one cell.
Another way to do this is to add another tableview inside this cell and increase its height based on the number of rows it has. But I am not sure about performance in that case.

Resources