UITableView embedded in view not sizing accordingly - ios

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.

Related

Layout errors when pinning UIView edges to UITableViewCell contentView

Got a problem here which is driving me crazy.
In a storyboard I have a table view in a layout and I'm trying to pin the leading, top, trailing and bottom constraints of a UIView to the prototype cell's content view. Straightforward enough.
Whenever I do this I get these layout errors for the UIView in Xcode:
"Need constraints for: X position, width"
"Need constraints for : Y position, height"
Constraints on that view (Superview is contentView):
In the storyboard I have the following set on the table view:
Row Height: automatic
Estimate (row height): 50
And on the table view cell:
Row Height: 50
I've seen a few SO posts and other walkthroughs that state that pinning the edges is what to do, and as I understand Auto Layout, I've provided the necessary constraints and info.
How can I fix this?
(BTW: I'm running Xcode 11.3.1.)
Update: The cells of this table view will be fixed height. I'm looking for the cell to provide the height for its content.
UIView by itself has no intrinsicSize - so you are telling the cell's contentView to pin to a frame of 0 x 0.
If you are going to add content to the UIView, set the constraints on the content to control the size of the view and the errors will go away.
In the meantime, give the view a height constraint and that will resolve the issue.
Edit
For fixed-height cells... try starting fresh.
Add a table view - give it desired constraints
Add a table view cell prototype
Add a UIView to that cell (give it a background color to make it easy to see)
Constrain the view at Zero on all 4 sides (constrain to margins)
Set the cell Row Height to 50 (un-check Automatic)
Here's how it looks for me:
It turns out I had the table view and cell constraints and settings configured properly; the problem was further up the view hierarchy.
The table view was placed in a vertical stack view, and that stack view's Alignment property was set to center (for the benefit of other views in the stack). This was causing issues for Auto Layout in calculating the width of the table view.
The solution here was to add an Equal Widths constraint from the table view to the stack view. Et voila! The errors on the UIView within the table view cell's content view disappeared.

How to set UITableView cells width to same as its parent/device width?

I have a UITableView cells that won't follow it's parent width. I found so many codes for how to change the height, however I couldn't found codes/ways to change UITableView cells to same as it parents.
Some of answer told that it would follow automatically. But in mine, the problem is the width can't follow device's width. Is there anyone that know how to change it?
The UITableviewCell width will always be the same width of the UITableView.
If you want to set the width of the cell same as the parent of UITableView, You should set the width of the UITableView same as that of parent
If you want to set the width of UITableView the same as it's parent view, set the trailing and leading constraints of your UITableView to it's superview with value 0
you need to remove the width constraint and aspect ratio that you have added to it. Instead of ctrl and drag, select the tableview and click the pin button and set the constraints as shown in image. Remember the trailing and leading values must be 0 for setting the width as same as parent view

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

Make custom cell's ( created with autolayout) height to zero

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];

Autosizing UITableViewCell: change height of a view inside a custom cell

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:

Resources