Change height of UIView inside of UITableViewCell (Storyboards) - ios

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;

Related

ios - UITableViewCell does not fill the entire width of the table

I think the cells don't fill the parent width because of indicated dimensions 300x300, but I don't know how to change it to parent size.
TableView has constraints on the boundaries of the device. I also tried set constraints for the cell but it didn't help.
You should set constraints in your image and on entire UITableViewCell xib just like in ViewController
This is a very basic design task. You really should take the time to go through a few tutorials on Auto-Layout.
The comment you posted: "Adding constraints in the UITableViewCell xib is not possible for me," along with the image, indicates you are selecting the Content View in your cell xib, but that is not where you want to add constraints.
You need to constrain the elements in your cell to the content view:
which results in a table view (at run-time) that looks like this:

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.

swift uitableview in uitableviewcell with auto layout always height of 0

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.

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.

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.

Resources