TableViewCell with multiple columns - ios

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.

Related

Label cut off on Table View Cell

The top and bottom text label for my tableView cell appears to be cutting off. It is not clipped in the Xcode preview or the storyboard view. How do I make the labels extend to fill the entire row?
I am using a stack view that contains the file name and file size labels.
There are a couple of "potential solutions":
set your constraints (especially the height constraint of the label) correctly
set your cell height correctly. (You probably want to use automatic height)
The layout would be broken if you didn't match these 2 conditions.
This thread will be helpful: https://stackoverflow.com/a/18746930/938380
Maybe you just forgot to set tableView.estimatedRowHeight = ...
Increase the height of label then it will look what you are expected size of the Label.

Horizontal UILabels inside a self sizing UITableViewCell

Question: How can I use constraints in a UITableViewCell using UITableViewAutomaticDimension when there are labels side by side which can each contain a different number of lines?
I have a table with cells that contain labels side by side:
I'm having a hard time getting the cell to size properly if one of the labels on one side have more lines than the corresponding label on the other side. One of the labels always gets cut off. If both labels have the same number of lines, the cell resizes perfectly and all text can be seen.
I've tried a number of constraint tweaks but nothing is working perfectly as desired.
I've tried adding hidden label for height...but it doesn't work well for the cell with 4 labels
I've tried placing the labels in a view but then the height of the view still needs to be calculated.
Currently the constraints are setup normally: top, bottom, left , right, labels are set to 0 lines etc and works great when both labels have the same text or same number of lines.
Is there any way I can do this with constraints? Or do I need to revert to the old height for row at index path? =(
There is a way to achieve what you want to do just using constraints. The trick is to set the relation of the constraints between the two lower labels and the bottom to Greater Than or Equal instead of equal.
Here is how I set the constraints:
Which has this result:
Or you could set the constraints between the labels top and bottom to Greater Than or Equal and leave the bottom constraint at equal:
To get this result:
I found a solution where I did not have to revert to using tableView:heightForRowAtIndexPath.
I added a label to the cell with the text color set to clearColor and called it 'heightLabel'. I added constraints to the heightLabel so the cell would resize based on the heightLabel's constraints.
Then whichever of the left or right labels had the most text, I placed that label's text inside the heightLabel.
The cells still were not resizing and I had to call 'layoutIfNeeded' on the cell before returning the cell in tableView:cellForRowAtIndexPath.
The heightLabel constraints are now what is being used to resize the cell and UITableViewAutomaticDimensions is working properly.
This may not be the best solution but it's working out pretty well. If there is a more proper way of doing this I'm open to suggestions.

UIScrollView with multiple multi-line labels and AutoLayout?

Is there a way to achieve this? I have tried literally everything and nothing has worked for me yet.
So basically what i want to do is the following: I have a scroll view with some labels in it. All the labels get their text from a server and I have set their number of lines to 0 so that they change their height according to the amount of text. However, this does not affect the scrollview content size(even though my labels have constraints set up to the bottom,top,leading and trailing of the scrollview) and the labels go off screen and I am unable to scroll down. Can someone point me in the right direction to how I would set up my constraints, my view hierarchy and etc?
Any help is much appreciated! :)
Late, but this solved it for me:
Set leading (I have a 32pt inset), trailing and top constraints. The trailing will not actually seemingly do anything..
Make the trailing Greater Than or Equal to avoid localization alert.
Finally, add a new Equal Width constraint to the label matching the scrollview. Use the constant to subtract the required padding (I used 64 due to mirror my leading inset).
And voilĂ ! The Label will align correctly both in IB and in-app.
In Scrollview the last view's bottom constraint is so important. You should set its priority to 250 and put it to Greater than or equal.
Remember you should only change the bottom constraint of the last view, which in my case it's the continue button.
I would consider using UITableView instead, it has several benefits:
It allows for reuse of cells, if all the cells look the same
It manages recycling of cells when the number of values you're getting from the server increases (decreases memory pressure when number of cells becomes substantial)
It allows for more flexibility with the content (it's quite often for design to change last second or to evolve over the course of the project)
Most importantly, UITableView support auto sizing cells (as of iOS8), you need to specify the constraints between the label and the borders of the cell
There are several resources to start with:
http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout
https://www.captechconsulting.com/blogs/ios-8-tutorial-series-auto-sizing-table-cells
http://www.appcoda.com/self-sizing-cells/
Use a container view in a scrollView
Add constraints to superview (leading, trailing, top, bottom, height,width)
Make IBOutlet of constraints that you are going to update.
Add you all labels inside that view.
Update constrains/frame of your label so that it fits the text.
How much you increase the label height you should increase the container height too.
If the label count is not fixed use custom label class to add subview.
Perhaps you should need to understand how ScrollView works in Storyboard with autolayout.

Adding multiple UILabels in view with constant height between them

I am trying to add some 5-6 UILabel's (Single line only) in a UIView using auto layouts. I have added constraints for top most & bottom most UILabel. The problem is variable screen sizes.
Just wants to know a better approach of adding those remaing (3-4) middle UILables (As I want to show equal space between all the UILabels).
Approach 1 - Add height constarint for middle lables & based on screen size change the height constraint constat at runtime.
Approach 2 - I tried adding >= and <= constarints but I am not getting the output as desired.
Any help, how can I keep constant height between all UILabels.
(Can be achieved via UIScrollView or UITableView but I dont want scrolling .. only using auto layouts:))
You can add dummy UIViews between your UILabels and set those dummy views' heights to proportional to their container.
Or you should be able to achieve that by setting your current constraints constants at runtime and then calling setNeedsLayout&layoutIfNeeded
You can't achieve that by playing with "less than" or "greater than" constraints.
Edit: You can also use UILayoutGuide to do that on iOS9.0+
You can use stack view from left window where you have button/label/view etc..... or also you can add it from constraints tools(see attached image)
If you are looking for the attached solution, please add below constraints shown in the third image.

Is there a way to fill a collection cell with two labels (fixed-scale-width) in xib?

I'm a newer to iOS, and start to use auto layout in xib.
These days I ran into an issue: Is there a way to fill a collection cell with two labels (fixed-scale-width, say, the scale is 3:1) in xib? If xib cannot do this, then how to do to achieve this?
And, I haven't turn to Swift yet, so the solution must not only for Swift.
Hope your answer, many thanks!
PS: Aspect Ratio can be the solution? But is it just a ratio for one label's width and height? as follows:
But, what I wanted is just let the ratio of widths of item1 and item2 be 3:1, no matter with the height!
Another problem is: if the cell height changed, but the width not, the ratio can keep to be 3:1?
Yes.
Create a xib
Add a collection view cell
Add two labels to it
Control-drag from the label to itself. Set the aspect ratio to 3:1
Do the same for the other label
Add the other necessary constraints to each label.
I had no problems doing this? Did you come across a specific issue?

Resources