How do I make constraints to UITableView with multiple cells with static and dynamic height? - dynamic

I searched a lot about this, couldn't find an answer and I am stuck with this problem for some days already.
I have a UITableView with 3 sections, each one with custom cells (xib). The first section must grow accordingly with one of its labels.
The second one is static, it shows up just an static image and basic height.
The third one is like a comment cell, with UIImageView, labels etc. One of this labels must grow till a limit height.
I managed to show the 3 custom cells, but the first and the second one (which are not static height), show only partially. If I set manually the Height of the row I can see it entirely, but all section cells grow and it is not what I need.
The constraints to auto layout, I keep getting some conflicts but could not find any solution, tried to debug the view stack, no success.
Custom Cell
Constraints of custom cell
Can anyone help me or point me to a direction?
Thanks in advance!

I had the same problem. I solved it like this. Set tableView estimatedRowHeight to minimum row height. Set tableView rowHeight to UITableViewAutomaticDimension.
1) For dynamic cell height - Give the label leading, trailing, top, bottom and height greater than equals to minimum height.
2) For fixed cell height - Give the label leading, trailing, top, bottom and height equals to fixed height whatever you wanted.
By this your dynamic height cells will grow and fixed ones will be fixed. If you did not get what i said then post a sample project with just this custom implementation on git or somewhere else. I will correct it.
Thank you.

Related

How do I keep two labels from overlapping, but have a dynamic width?

I have two labels on the opposite sides of a UICollectionViewCell that both have dynamic widths. As seen in the screenshot bellow, the "Cream Cheese" and "$15.79" are automatically overlapping because of their dynamic widths.
I want to keep both of those label's dynamic widths, but if the two come into contact, I want the label on the right of the UICollectionViewCell to be the preferred label. That would mean that the width of the label on the right is minimized depending on the dynamic width of the right label. Does anyone know of any ways to accomplish this?
If you are doing this through Interface Builder, you should have a constraint for horizontal spacing between the two labels...
...and give a higher content compression resistance for the label on the right.
That way, it will expand and the longer label will have a shorter width, just as you'd expect.

How to use Auto-Layout properly in this case?

I have a UIView and a UITableView. I'm trying to align them in a way so they are sticked to each other. The UIView has a fixed height and I want the UITableView to consume the rest of the horizontal space.
I applied a set of constraints which got me pretty close to what I want to achieve but there's a problem that I don't know how to solve. The layout is OK in the portrait orientation but there's a gap between the two elements in the landscape mode. Please see the screenshots below.
Here are the constraint setups for the elements.
UITableView: (all constants are set to 0)
UIView: (all constants are set to 0 except for Height)
Thanks in advance.
I had the same issue of that space appearing above the top cell of a UITableView.
Like yourself, I looked into the constraints and the properties of the table, though the only way I could fix the issue was to delete the UITableView altogether and re-insert it.
Once re-inserted the gap was gone.

dynamically load UICollectionView in xcode

We have a label followed by a UIViewCollection. The height of the label is dynamically set based on its content. Based on the height of the label, we want to start the UIViewCollection as soon as the label ends. We are tracking the label x, y, width and height and adding some padding to y in the code to calculate the start of UIViewCollection. However, UIViewCollection does not move at all. Is it because of the specified storyboard? We tried it in ViewDidLoad and ViewWillAppear.
Also, if we replace the UIViewCollection with a label such that there are 2 labels one below another, we are able to dynamically load the start of the 2nd label if the code is placed in ViewWillAppear.
Please can someone help with moving the UIViewCollection up or down based on the previous label height?
Thanks in advance.
It may be because You use autolayout. You can switch off autolayout or set translatesAutoresizingMaskIntoConstraints YES for UIViewCollection, and than set current location
If you're using auto layout this is easy to do, and you don't need to do anything in code to keep track of the label's height. The label should have a height constraint to the top of the view and a spacing constraint to the top of the collection view (the length of that constraint will give you whatever padding you want between them). The collection view then needs a constraint to the bottom of the view. This setup will cause the collection view to get shorter when the label gets taller. If instead, you want the height of the collection view to stay the same, but move down (assuming that it's short enough that there's room to move down), give the collection view a fixed height constraint, and remove its constraint to the bottom of the view.

UIScrollView, pagingEnabled, scrolling inertia, and sub sections of the UIView width

In my app I'm using a scrolling canvas composed of equally sized cells.
The number of rows is unlimited, but the number of columns is fixed to 13.
At anyone time, I have exactly 7 cells fitting the width of the view.
I wrote my own tiling UIScrollView and everything is fine and works fine except for one thing:
I want to enable "paging" on the cell boundary, not on the "page" boundary.
In other words I want the horizontal scrolling to move no more than "one cell at a time". This is a usability issue.
In a standard UIScrollView with pagingEnabled=YES, there is some inertia when moving from one page to the next, but then the page is defined as having the full width of the view. I want to define the notion of page as 1/7 of the view width, such that I get the same kind of inertia when moving the display from cells 1-7 to cells 2-8, to cells 3-9, etc...
How would I go about that?
I found this and this, but this a related but different problem.
"Try making your scrollview less than the size of the screen (width-wise), but uncheck the 'Clip Subviews' checkbox in IB."
More info here.

Transparent NSTableView

I want to customize my NSTableView. My overall requirements are like this:
Each row will have one image and some text; images and text could be different.
Some cells might not have an image.
Row height is dependent upon some external factor.
A cell shouldn't draw the background, it should show the NSTableView background.
So far I am able to draw transparent cells with some text. I Googled and found out I need to customize each cell. Now I have this question: should I maintain two columns or should one column be okay, having one image on the left hand side and text adjacent to that?
I also understand that I need to override two methods:
- (void) drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
So for each cell, I need to draw/construct the cell -- can anyone guide me? Am I going in the right direction? Can I achieve a transparent background with dynamic height with the above approach?
Each row will have one image and some text,
So, two columns—one with an image cell, the other with a text field cell.
images and text could be different,
I should hope so.
Some cell might not have the Image,
Not a problem.
Row height is depend upon some external factor,
Be the table view's delegate, and it will ask you what the row's height should be.
Cell shouldn't draw the background,
It won't unless you set it to do so.
it should overall it should show the NSTableView background,
The table view will draw its own background anyway, which you can set in IB's Inspector. If you wanted the table view to not draw a background, you would set its background colors to the clear color.
You don't need a custom cell for any of this.