How to create UITableViewCell with dynamic cell height based on its content - ios

I was trying to explore the new feature of dynamic cell height using auto layout, introduced in Xcode 7 using this link. I have one UITableViewCell like this below
I want cell to adjust its size automatically based on the content in textView and size of image. I set all constraint and given estimatedRowHeight and UITableViewAutomaticDimension as tableview row height. But when i run the app i'm not able to see the UITextView below. Which means that cell height is not getting adjusted dynamically. Do i need to set the cell height programmatically or still i can do it using Auto Layout.

For TextView, it must not be scrollable.
Also, Your constraints are should be provided in such a way that, TableViewCell should get height automatically(there must be vertical spacing constraints between each component, Height of the each component must be there(it may be explicit of implicit height, but it must need to have height)).
Also, estimatedHeight you are providing must be near to actual average height.
If your tableViewCell is getting above things, then and then only it will get dynamic height based on its content.

Related

Creating different cell sizes with dynamic height and full height

I understand how to create custom UITableViewCells with a dynamic height in iOS8+.
Within our app we have some full size height cells, for error/loading states. Then we have some dynamic height cells for the actual correct content.
What is the correct way to handle a full height cell using the dynamic height approach? Previously we would use the heightForRowAtIndex and set the height to the be the tableView.frame.size.height for example.
Now we need to have some cells which would be fullsize, some which might be 200 and some which might be 240 as an example.
Just setup the constraints in your full size cells to calculate to the correct height. You can either give them a constant height constraint or base it on it's content.
That way you don't have to mix absolute and dynamic heights.

UICollectionViewCell inside UITableView Cell and Auto Layout

I have anUICollectionView inside aUITableViewCell and I have some issues regarding to Auto layout.
That's the hierarchy of my views.
UITableViewCell
|
-------ContentView
|
-------MyView
|
|-----UICollectionViewCell
Until here, all views are pinned up in his superviews., e.g: trailing space=0, leading space=0, top space=0, bottom space 0.
The height of myUICollectionViewCell is dynamic, based on the number of itens, which I draw usingUICollectionViewLayout. So it has a height constraint which is >= 100.
After the Items are in place, I call one delegate to update that height constraint, so myUITableView is able to calculate the right height for theUITableViewCell.
Even though this are working fine, sometimes I get the auto layout error above.
How can I avoid this?
The cell's contentView has a fixed height (44). That's causing the occasional conflict between the storyboard, and Auto Layout's derived height.
Self-sizing is the best way to go. What you want to do is to get your "collection view" to determine its intrinsic size, so Auto Layout can automatically handle the variable cell height for you.
See the detailed walkthrough by smileyborg in his answer to Using Auto Layout in UITableView for dynamic cell layouts & variable row heights.
He uses labels but the principle is the same. The cell asks its contentView to lay itself out. The contentView asks the label to lay itself out. The label determines its height. Auto Layout determines the contentView's height, based on the label's constraints, and the cell determines its height, based on the contentView's height. The point is that the label doesn't have a height constraint, and your "collection View" would no longer need one either.
Once you adopt self-sizing, it eliminates a lot of code (and size constraints), as containers manage their size, and constraints simply handle spacing.

How to use the new auto sizing feature of UITableViewCell in iOS 8?

I have a custom UITableViewCell subclass, and say there is a UIPickerView inside of it. As far as I know the cell requires constraints on both its top and its bottom so that the height can be calculated. The problem is when I specify only the top and bottom constraints, the height of the cell displayed is like 44. So I added a height constraint for the UIPickerView to be 162, then the console says all these constraints cannot be satisfied simultaneously.
So how can I let the estimated height to be 162 so that the UIPickerView is displayed properly (Without overriding the height function of UITableViewDelegate?)

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.

With automatic UITableViewCell height in iOS 8, is it possible for it to work if the cell's contents are embedded in a UIView?

My UITableViewCell subclass has several UILabels in it of varying length, and with iOS 8 and Auto Layout constraints in the cell the height is now automatically configured and cell heights can be dynamic.
However I want to embed all the contents of the cell in another UIView. Is it possible for the cell's height to still be dynamic? As in the UIView that the cell contents are embedded in will grow and shrink automatically based on the content and thus the cell will as well? Is this possible?
Dynamic height works by performing an Autolayout pass on the cell's content view. As long as the constraints are in place for a height to be calculated, the specific view hierarchy should be irrelevant.

Resources