I want to make height of a custom tableview cell to zero, which was created with autolayout. Basically I have container view added as subview to cell.contentview. Container view's top, bottom, left and right are pinned to content view's top, bottom, left and right. Under certain condition I want to make height as 0.
I iterated through content views's constraints and found this constrain causing the problem.
NSLayoutConstraint:0x1369d8670 UITableViewCellContentView:0x1369b1a90.height == 48
This constraint is added by default by tableview.
Things I Tried : I removed all subviews of container view. But this does not make cell height to zero.
Things I don't want to do:
I dont want to reload entire tableview coz I will be having many rows with many subviews.
I don't want to delete row
How can I set height to zero?
You can reload particular row so it will call tableview's delegate method for height change heightForRorAtIndexpath. You can set the height 1 when you programmatically reload particular row. You can store temporary indexpath which you want to make zero.
Clip all the subiviews by clipsToBounds=YES
call [self.tableView layoutIfNeeded];
Related
I am trying to add table view that is contained into a view, this view has a width constraint and the height of the view will be dictated by the content height of the table view. However, I am unable to get this to work.
My attempt:
I create a view with a leading, top and width constraint. Then I insert a table view inside this view, this table view will have a leading, top, bottom and trailing constraint to the view (it's superview). I also set the row height and the estimate. When running the code, numberOfRowsInSection gets called but cellForRowAt does not, my guess is because the height didn't solve properly and the table has a height of 0. How can I fix this?
Thanks.
luis,
UITableView inherits from UIScrollView. So You cant expect the tableView to provide the height for its superView. In fact its vice versa.
TableView's frame will be decided by the superView's frame where as the tableView's contentSize will be decided by the data that you have provided.
So your best bets
Solution 1:
Add a height constraint to the View (SuperView) and create a IBOutlet to it in your VC.
Set the height for each cell in tableView lets say as 50.
Once you get the data to show in tableView before calling reloadData() on tableView set the constant of View's height constraint as
viewsHeightConstraint.constant = (numberOfRows) * 50;
yourView.layoutIfNeeded()
Solution 2:
If you are planning to use automatic height for cell then set the height of view based on the ContentSize of tableView. But to do that you have to get the data first, reload the tableView with the data and once done now get the content size of TableView and set the frame of your view accordingly.
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.
I have a view1 which can have dynamic height depending upon content with in it. Just below that view, I have to show the table view. Top space constraint of tableview is view1 and bottom constraint of tableview is view 3 that has fixed height and stick to the bottom.
The problem is that I can neither set height constraint of view1 nor tableview as top view can be dynamic and tableview have to take remaining height that varies in different device and I am getting error:
"Need constraint for Y position or height" for both view 1 and tableview. Although I have set costraint for y position for all views.
How should I solve this.
Add a constraint so that the top of your tableview is adjacent to your view 1.
Create a constraint for view 1's height. Doesn't matter what value you pick, just pick something.
Create an outlet for your constraint in step 2.
You should now be able to programmatically update the constraint in step 3 to have whatever value you really want for the height, and the rest should happen like magic.
4a. You might need to call layoutIfNeeded to make the view redraw and relayout things.
I have a Detail View Controller, which has different text lengths according to which cell you press in the Main View Controller, of which it segues to. How do I set the cell's height according to how much content is in it?
FYI, the cell consists of 3 labels and 1 image view. It is vertically set as UILabel, UIImageView, UILabel, UILabel.
I am using Swift.
This is what happens when I add the auto-layout constraints:
The way to do it is to use auto-layout. You need to set up vertical constraints from the top to the bottom of your cell:
Distance between the top edge of the cell and the top edge of the first label.
Distance from the bottom edge of the first label to the top edge of the image view.
Distance from the bottom edge of the image view to the top edge of the second label.
and so on ...
Auto-layout will take care of sizing the cell depending on the size of the content you put in your labels and the image view.
You should also remember to set the estimatedRowHeight property on your table view to some meaningful value. This will help the table view defer some of the calculations of the content size to when the user starts to scroll.
Also, set the rowHeight property on your table view to UITableViewAutomaticDimension.
There are 4 things you'll need to do:
Set your tableView:heightForRowAtIndexPath method to return UITableViewAutomaticDimension.
Set your tableView:estimatedHeightForRowAtIndexPath to return the average height of the cells. You can set it to something static like 100.
Set your Auto Layout constraints so that all subviews are tied to each other vertically, and the top-most and bottom-most views are tied up to the cell's contentView.
Set the label's preferredMaxLayoutWidth property either in code or in your Storyboard to be equal to the label's width.
To fix this, I added the sizeToFit() property to my label.
I searched a lot for a solution but can't find anything to solve this particular case.
I created a custom cell in a UITableView, with these elements in order from top with the constraints:
imageview (constraints top screen, fixed width, fixed height, centered horizontal)
top-label (constraints top imageview, fixed width, centered horizontal) Line set to 0.
view (constraints top label, fixed width, centered horizontal) The height is set to 0.
button (constraints top view, fixed width, fixed height, centered horizontal)
bottom-label (constraints top view, constraints bottom screen, fixed width, fixed height, centered horizontal)
The cell is autosized in height correctly, also if I insert a text for top-label very long.
Now I want to attach an action to the button to enable the resize in height of the view, like an accordion. So I'm trying to change the height with no success, anything change also if I reload the tableview.
I tried to set a constraints to the height of the view, but If I change that all the content move but the height of the cell doesn't change.
The only way I can have the view changing the height is setting the the rowheight of the table, but that change all the cells of the table and I want to open the view of only one cell.
Is there another way to do that?
Thanks in advance
Ale
EDIT:
For clearance I've solved using the delegate method suggested by noobular, setting the view to 0 height permit to have that expanded when the height of the cell change.
I achieved a similar effect by these methods. First, I determine a constraint that will represent the height of my cell. I'm using the height constraint of a decorative bar on the left hand side that's pinned to the top and bottom of the cell. It's important that this constraint belong to an object that is a child of the cell's content view and that will be on-screen in both open and closed states. This constraint is connected to a property on the cell it will control.
When I want to change the height of the cell, i.e. to 100, I can do so as follows:
cell.heightConstraint.constant = 100.0
cell.layoutIfNeeded()
tableView.beginUpdates()
tableView.endUpdates()
The calls to beginUpdates and endUpdates force the tableView to layout its cells again, thus expanding or contracting the cell. Note that I'm actually calling the first two lines in the cell's own setSelected:animated: function and the remaining two in the table view controller's tableView:didSelectCellAtIndexPath: method. This has the effect of expanding a cell when it is selected.
Edit: For auto sizing cells where you want UIKit to automatically calculate the height, I would suggest researching UITableViewAutomaticDimension
Implement this method for your tableView delegate:
tableView:heightForRowAtIndexPath: