swift uitableview in uitableviewcell with auto layout always height of 0 - ios

I have a UITableView defined in a storyboard with a UITableViewCell that contains another UITableView as part of its content with a UITableViewCell defined in it all in a storyboard.
IB image
Both content views are fully specified by auto layout. The nested table however always has a frame height of zero. If I turn on a height for the nested table you can see all of the content so I know it is there; however without the hard coded height its always 0. I've done this before but for whatever reason I am missing something.
The top level table works fine. The nested table properly loads its cells. I've done the trick of
removalTable.estimatedRowHeight = 127
removalTable.rowHeight = UITableViewAutomaticDimension
removalTable.setNeedsLayout()
removalTable.reloadData()
I'm not really new to SO but for whatever reason I can't get my openID to work.

I have seen this error a lot of times.
it is probably because you are not setting good constraints inside the cell you are displaying. What you need to check?
you have an anchor to the top and the bottom at least.
you have a fixed height in one view of your content.
Maybe this post can help you: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
I hope it helps you.

Related

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.

UITableViewCell of mixed xibs and programmatically generated UITableViewCells

I have to integrate a UITableViewCell generated programmatically from code like so:
UITableViewCell *newCell = [[UITableViewCell alloc] initWithFrame: CGRectMake(0,0, screenWidth, 150];
However the rest of the cells in this table view are generated with xibs that rely on autolayout, and so the original programmer only used the estimatedHeightForRow method rather than the heightForRow.
My programmatically generated table view cell is all botched (has the default estimated height of 64 rather than the frame's height of 150) unless I implement heightForRow whereas the existing cells are botched as soon as I do implement heightForRow in addition to estimatedHeightForRow. Is there a way around this conundrum?
Thanks for any advice.
Firstly, you should use UITableViewCell's designated initializer - initWithStyle:reuseIdentifier: instead the UIView's initWithFrame:. However, I don't think that is the cause of your problem as I have tested initWithFrame: and it appears to default to default style and no reuse identifier.
You haven't said what you are doing with the cell once you instantiate it. Are you using the default layout or adding your own custom views? The default layout (with just a line of text) appears to work correctly in my testing, with the cells being sized according to the amount of text in the textLabel.
If you are adding custom views you need to ensure that the contentView has vertical constraints that fully define its height. For example, if you have two UILabels vertically aligned then you will need a vertical constraints such as #"V:|-10-[label1][label2]-10-|". If the constraints are not fully defined then contentView will collapse to zero and your views will appear overlapping the next cell.
To give a little more detail, when using self-sizing cells UITableView does not look at the frame of the returned cell but rather calls the cell's systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority:. This results in the Auto Layout engine analyzing the cell's constraints to calculate the appropriate layout size. UITableView then sets the cell's frame according to that size and its position in the table.
I would put an if statement in your height for row method and set the heights to their corresponding cell. It would also be easier if I could see code.

Change height of UIView inside of UITableViewCell (Storyboards)

I'm trying to create a master UITableViewCell with storyboards. The idea is that this cell contains all the possible subviews, but not all will be shown depending on the data for each cell.
I calculate the appropriate height for each cell depending on the data using the heightForRowAtIndexPath and this is working fine.
What I'm trying to figure out is how to adjust the height of subviews to zero inside the tableViewCell to effectively hide those elements while still respecting the relative constraints I set up (unlike removing the subviews from the superview which messes up all my constraints).
So basically I can't seem to figure out how to update the height(frame) of a subview (UIView) inside a UITableViewCell using storyboards and autolayout.
Any guidance would be awesome, and please let me know if I'm not using the best paradigm or thought process for the desired effect.
Best,
emjoseph
You'll need to use NSLayoutConstraint's. If your cell subviews do not already have fixed heights, you'll need to add them.
Once you've added those constraints, create references to your header file for those constraints. To do this, double click the height constraint so that it reveals itself in your ViewController structure, then control click and drag into your header file.
When you've decided which view to collapse, access your newly declared NSLayoutConstraint and edit the constant value.
Example:
subviewHeight.constant = 0;

Swift: UITableViewCell size to width of parent UITableView with autolayout enabled

I'm experimenting with autolayout and am running into trouble with UITableViewCell since they're created at runtime. My cells are loaded from a xib from the main ViewController. This xib has View mode set to Aspect Fill.
I've read about different ways to do this online and have yet to get any of them working. What's considered the best way to handle this?
It looks like your constraints aren't set properly, as the cell is shorter than the image's height.
Using AutoLayout and self-sizing cells is the easiest way to handle what you want to do. Once your constraints are setup properly for your custom cell, tableView:cellForRowAtIndexPath: can call dequeueReusableCellWithIdentifier:forIndexPath: and all the subview layout will be handled for you.
See the detailed walkthrough by smileyborg in his answer to Using Auto Layout in UITableView for dynamic cell layouts & variable row heights.
He also provides workarounds for the minor issue with the initial cell width being based on the storyboard cell, instead of the tableView width. I worked around it by setting the cell's initial width to the tableView's width, as Rasputin had suggested.

Content of UITableViewCell falling outside of cell

I've been fiddling around for a bit to try and get this to work but for some reason I can't get the content in this tableView to stay inside the cell.
The following image is a screenshot of my storyboard where I define the tableView. You can see that there is amble space.
But then in the simulator you can see that the content is leaking out of the cell into other cells.
If I scroll then the cells crop the text but do not grow in height.
Not sure what kind of information will help out with this.
I have constraints added to the cells defining height, not width, as well as trailing edges and tops of the cells. Nothing crazy that should be causing this.
I'm defining the row height in the storyboard as such:
Define your row height from your TableView attributes inspector and uncheck the "Custom" option in your UITableViewCell as you show above.

Resources