swift iOS full height table inside a table cell - ios

Goal
I'm trying to create a table where each cell has a table inside of it. These inner tables are not scrollable and need to be full height based on their content size.
Code
Click here to see my repository with an example of what I'm trying to accomplish.
ViewController
Displays a table with a hard-coded array of food groups
Each table cell is a FoodGroupCell
FoodGroupCell
XIB with a label and a table (has a corresponding swift file)
Each table cell is a FoodItemCell
Has an #IBOutlet for the table's height constraint
Uses a KVO on contentSize
updates the height constraint to the table's content size
calls updateConstraints and layoutIfNeeded on the table cell
FoodItemCell
XIB with a label
Label has a height of 200
Problems
If I have a height constraint and no bottom constraint on the inner table the cells do not display properly.
If I have a height constraint and a bottom constraint on the inner table the cells appear, but do not adjust their size based on the label's 200 height.
This is obviously not ideal having a bottom constraint and a height at the same time. It does not seem necessary and xcode throws a bunch of warnings because of it.
Can anyone shed any light on my issue? Any help would be greatly appreciated.
Update
The problem was that I needed to update the constraint priorities, so that the height and bottom constraint could work together. I have updated my repository with the changes.

You may be able to work around the constraint issue by adjusting the priority of one of the two constraints so that it can adjust itself without being blocked by the constraints all having top priority.

Related

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.

UITableView - We're considering the collapse unintentional and using standard height instead

I am trying to use the self-resizing cell in one of my custom cell in UITableview. This is for the varying address field so as the address length increases the table cell should also increase with the UILabel.
I am using the following constraints:
The error in console is:
Moreover, the table view is not showing this cell because of this. I have given :
UITableViewAutomaticDimension
For both estimatedHeightForRow and heightForRow
UILabel number of lines as 0
What is the issue and why it is happening?
To calculate the height of UITableViewCell, vertical constraints top and bottom of the corresponding subviews which you are considering relevant for height calculation must be connected to the other subviews in such a way that it eventually connects top and bottom constraint to the superview ie UITableViewCell.
Add top and bottom constraints to Main office label and other subviews also.

Autolayout. Set two views vertically with dynamic height

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.

TableViewCell with multiple columns

I have an application in which I want to have a tableView with custom tableViewCell that has five columns.
I want them to be like 20%, 30%, 15%, 20% and 15% of the whole cell's width, because I want my application to launch on iPhone and iPad.
What is the best way to do this?
Now I have created a tableViewCell prototype with 5 labels. I also added constraints to them but the width constraint is just a constant. Also when I added width constraints to labels, some collisions appeared there and I don't know how to fix them.
Here is a screenshot.
You can create table view cell with collection view. By that, you can get different part in cell as well as you can change each column width programmatically based on your needs.
Thanks
A table view has only one column and allows vertical scrolling only. link
If you want to use a few columns, you can use UICollectionViewController
I also added constraints to them but the width constraint is just a constant. Also when I added width constraints to labels, some collisions appeared there and I don't know how to fix them
To fix it change width constraints to be a multiplier of superview width.

Resources