UITableView resize cell based on content - ios

Hi I need help with understanding how to resize cell based on its content.
So first of all of course I found many links:
iOS: Multi-line UILabel in Auto Layout
https://www.youtube.com/watch?v=CkvZEJ7dIfw
https://github.com/williamhqs/GSTableViewDynamicHeight
As we said about git example (last link) I can't understand how to change some label and make it works. For example if I delete UILabel and create new one and bind it with content property (content - it is IBOutlet property of bottom label). I seems lost some setting and cell won't stretch.
So I think I don't know to much understanding how to do it.
What I want to understand:
How to setup auto layouts in the storyboard or programmatically.
Which thing I should handle programmatically to make it done.
How the preferred size affects on label. Do we need every time use bonds of superview as a preferred size?
Also in the git example we have one label that changes itself size. What if we have 2 UILabels with dynamic content how to setup it?
If you have some links or videos please drop them here, because I really stuck. Thank you!
I know it's duplicated question but I can't understand how to setup it.

To make Self Sizing Cells works there is three steps:
Setup correctly the constraints of the cell (most important the top and bottom constraints)
Specify the estimatedRowHeight of your table view
Set the rowHeight of your table view to UITableViewAutomaticDimension
for step 2 and 3 it's so easy just add these two lines in you viewDidLoad method
tableView.estimatedRowHeight = 44.0 // or whatever you want
tableView.rowHeight = UITableViewAutomaticDimension
for step 1 which is the most important part of self sizing process
what you need to keep in mind is that "self-sizing needs to get its size from your constraints."
Basically we need to make sure that top space and bottom space constraints of our cell is correctly set up here is an example :
just two labels with the constraints top,bottom,right and left space to each other and to container (see image) the UILabels Lines property is 0 so that you allow have multiple lines
Another example from a project that I am working on:
bottom line:
The label constraints should push against the size of the table view cell and make it taller. So always check the top and bottom constraints.
Also you may run intro trouble working with UILabel with multi-lines because
The intrinsic content size of UILabel and NSTextField is ambiguous for
multi-line text.
so make sure always to setup the preferredMaxLayoutWidth property of yout label:
- (void)layoutSubviews
{
[super layoutSubviews];
self.contentLabel.preferredMaxLayoutWidth = self.contentLabel.frame.size.width;
}
for more infrmation about this issue here read :
iOS Autolayout multi line UILabel
iOS multi-line UILabel in Auto-layout
preferredMaxLayoutWidth
And for more information about Dynamic Table View Cell Height in general here is some useful resources:
UITableView Tutorial: Dynamic Table View Cell Height
Understanding Self Sizing Cells and Dynamic Type in iOS 8
Self Sizing Table View Cells
Mysteries of Auto Layout, Part 1 (wwdc 2015)

Not sure if it will apply to AutoLayout, although when laying out a cell programmatically, running "layoutIfNeeded()" fixed my cell content stretch issue.
layoutIfNeeded was ran at the end of a configure method, used to map values (view model) to UI objects (views)

Related

What actually defines the height of a UITableViewCell?

I'm working on an iOS App right now and I want to build a view controller that uses a UITableView to create new events in a calendar (very similarly to how iOS handles event creation in the system calendar, actually). The table view has two sections, the first section holding a date picker and the second section holding two custom cells for entering an event name and notes via a text field and a text view. After playing around with them I managed to force-set them to the right size, but in the process I realized that I don't actually understand how iOS calculates individual cell heights, especially in a table view with multiple sections and multiple custom cell classes. So far, I've found a number of things that seem to play a role:
Contents of a cell, e.g. a text field and its constraints
Hugging priority and compression resistance priority of a cells content
Settings for row height and view height in the size inspector of the cell itself:
Arrangement and Autolayout settings in the size inspector of the cell
Settings for the rowHeight and estimatedRowHeight properties of a UITableViewController
The more I look into it, the more complex and confusing it all gets. Maybe one of you can shed some light on this shady bit of Swift magic?
Basically, the rule is that if the table view's rowHeight is UITableView.automaticDimension, then as long as the estimatedRowHeight isn't 0, you'll get automatic row heights, meaning that the height is determined by the cell's autolayout constraints from the inside out.
The settings can be made in respect to the table view as a whole (in code or in the storyboard) or for a single cell using the height delegate method.
Add your constraints in the cell in right way.
don't use tableview "height for cell" delegate method.
use this in your viewDidLoad
self.tableView.estimatedRowHeight = 44.0
self.tableView.rowHeight = UITableView.automaticDimension
I would say that table view has a bit tricky.
Originally it needed to know size of cell before the cell was created.
The height of cell is defined by UITableViewDelegate optional function tableView(_:heightForRowAt:)
If this function is not defined (or delegate is set to nil) then it will take value of tableView.rowHeight
For performance reasons there was also added tableView(_:estimatedHeightForRowAt:) and tableView.estimatedRowHeight
The idea was not to calculate height of every cell during fast scrolling (such calculation may be costly) and use height that is good enough.
So that are the basics before constraints layout.
Then magic came. You can return UITableView.automaticDimension as height (by delegate method or by setting tableView.rowHeight). It will force tableView to calculate height from cells' constraints (note that constraints must define that height so very likely you want to set content hugging and resistance priority of every label, and you will encounter 'errors' in storyboard/xib).
Since that operation is costly you Apple forces you to specify estimated height by yourself. Also it's important to set that value to something that makes sense, otherwise things like programatically scroll won't work correctly.

Swift dynamic table text goes outside cell width

i just created a uitable view with dynamic cells, and as you can see with the insets, the cells have a correct width. But as shown on the image, the label goes out of the screen. I've set truncate tail, fixed font size... I can't manage to get this text truncated.
Should i do it programatically when creating the cell.text.value?
Help is very much appreciated here.
You might not have set frame in LayoutSubviews(). Initially when tableviewcell loads it always gives same width value but when layoutSubviews gets called it gives correct frame.
If you have done it in storyboard then you might have given hardcoded value.
Use Custom TableViewCell's
You can design the way you want and also you can set label/title width height font etc easily .
Make sure you register your tableview cell in viewdidload() or else it will crash
Here are two stack views, each with the same content - the text of the top label is this is a really long label that will extend past the right edge
The top one does not have a Trailing constraint.
The bottom one does have a Trailing constraint of 8.
That really looks like the issue you are running into.
Solved the issue. Initially i had dropped a table view inside the view controller and set the prototype cell to 1 in IBinspector. For an unknown reason, the constraints were not working. Ended up setting protoype cells to 0 and dropping a new table view cell from the object library.
That did the trick.

Is it possible to make an UILabel, UITextView, or similar that always resizes its height to fit its content in Interface Builder without truncating?

I've got a xib that I'm using for table cells (of variable heights, in a vertical UITableView), and each cell is populated with data that has an image and variable length text, but likely with different amounts of lines. I've tried wrapping them in an horizontal UIStackView, but I've found that there's no text view that resizes its container to fit variable multiple lines.
Both UILabel and UITextView truncate their contents seemingly arbitrarily in Interface Builder, while resizing the image's height makes the UIStackView container grow and shrink accordingly to wrap its contents.
Is there any view or a way to add a custom view that dinamically resizes its height according to its text length, without truncating?
I think this is what you want :)
There are loads of tutorials on how to achieve it :) Anyway lemme explain steps here, hope it helps :)
Tip:
Make use of tableView's estimatedRowHeight and set it to UITableViewAutomaticDimension and let UITableView do the magic for u :)
Step 1:
Either create cell xib or create a prototype cell on UITableViewController. Whichever you feel comfortable with. Creating a cell xib, iOS expects you to register the xib with reusable identifier in ViewDidLoad() I am planning to avoid it hence creating a prototype cell in UITableViewController :) Lets provide it a reusable identifier :)
Step 2:
Drag UIImageView and UITextView and apply constraints as shown :)
This answer assumes your imageView height is fixed :) In case it is not tweak the answer a little :)
Now the imageView has H:|-(8)-[ImageView]-(8)-| and V: |-(8)-[ImageView]
and finally height constraint of 100 :)
Now add TextView below it :)
TextView has no height constraints :) It just has top, bottom left and right constraints :)
Step 3:
Now select textView, and carefully uncheck Scrolling enabled, shows horizontal and vertical indicator :)
Step 4:
In viewDidLoad of your tableViewController or ur viewController write :)
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;
Step 5 :
Now sit back, relax, run the code, get excited and please comeback and accept answer :)
EDIT
In case wondering what else I might have written in UITableViewController :) Just tableView data source methods numberOfSections,numberOfRowsInSection,cellForRowAt thats all. Nothing else required :)

UITableviewCell Dynamic Height Programmatically with the use of UITableViewAutomaticDimension

Hi Guys here i am suffering issue of dynamic height of UITableview Programmatically. I have set Autolayout of all view without any error
but lblDesc (UILabel) not expand with its size according to content. plz guide me
sometimes I do not need btnVideo (UIButton) so i have to hide btnVideo(UIButton) and dynamically all social networks UIButton come after lblDesc (UILabel)
Here i attached my code with only one view
UITableViewAutomaticDimension
https://drive.google.com/file/d/0B5mabdphYDhzWG9UMzM2MTV6cms/view?usp=sharing
You should use UITextView instead of UILabel.
Btw, never forget you setup constraints between text view and container views for all edges correctly, so according to your text size, the whole cell will be resized dynamically.
I have reviewed your code. tableview has correct setting.
Just setup correct layout constraints so that table view could know how it should change the height of cell.
Cheers

Managing dynamic height for UITableviewCell using iOS 8

I am trying out autoLayout features for dynamic height for UITableViewCell in iOS 8, I followed this blog http://www.appcoda.com/self-sizing-cells/. So here they mention that from iOS 8 managing dynamic height is hassle free.(In the link they have used swift, but I am using objective C)
Now after doing autolayout from xib, then we need to just write this lines of code in viewDidLoad
- (void)viewDidLoad
{
m_tableView.estimatedRowHeight = 83.0f;
m_tableView.rowHeight = UITableViewAutomaticDimension;
}
This is fine and my tableView height changes dynamically based on the content.
I want to know how to have more control on UITableViewCell height.
1) In my app, I want the cell height to be based on the content(data), but I need to have a default height, which will be applied if the content height is less than default height.
2) I want to know what will be the final height of the cell, before displaying to the user, because I want to add some UI elements below each cell.
So what functions I need to use to fulfil my tasks.
You would need to make sure your Storyboard prototype cell has NSLayoutConstraints from top to bottom. Dynamic cell sizing compresses whatever layout constraints you have in place to their maximum compression given the content set in cellForRowAtIndexPath to calculate the final rendered height of that cell. It is very convenient but it also means you need to be careful about setting your constraints correctly.
If you are adding any subviews programmatically, you need to ensure the same rule of thumb is true- there is a complete, deterministic set of constraints from the top of your cell's contentView to the bottom. Additionally, you will need to set any subviews' added in code translatesAutoResizingMaskIntoConstraints property to NO.

Resources