Creating different cell sizes with dynamic height and full height - ios

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.

Related

How to auto adjust cell’s heights depending their text inside?

I am using a tableview to display a list of articles. Each cell needs to show an image and a text that has a brief descriptions of articles. What I want is to adjust the cells height depending the description length.
I know that I can resize the cells with the delegate method heightForRowAtIndexPath but I still don’t know which height to return.
Any help?
Ok, you can do that just using Interface builder. On the Tableview properties click “Row height” automatic.

Then you can use a UILabel with property lines set to 0 to display your text. When adding the constraints to the cell you need to make sure that the height of cell depends on the intrinsic content size of the label, basically is the size of the content (the text inside the label).
For example:

Here I have added a UIImage with constant height and width and with top and leading space to 0. Also I added a UILabel with top/bottom/leading/trailing space to 0. Here the cell height will depend on the UILabel intrinsic size that is what you want. Also to prevent glitches with short texts you can add a minimum height size to the UILabel.
Of course this is an example, you can achieve the same using other constraints. The main thing here is to be aware of the concept of intrinsic content size: a predetermined size of a view based in its content.
Let's say you have a textView in each cell. The width of the textView is fixed. And you know the text for each cell. Base on the info above, you can get the height of your textView by:
textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
Then you should be able to calculate the height of your cells base on your layout and stuff.
The question is answered but just in case you want to know... You can return UITableViewAutomaticDimension in heightForRowAtIndexPath.
Nevertheless you should google more before asking a question that has been answered multiple times on the web (and on stackoverflow) ;)

UITableView content size not getting proper

I have UITableView for display for chat message list. I am using 5 type of different with dynamic height. Minimum cell height from 20 to maxHeight( approx 1000 px). UITable view cell dynamic height is set by "self.messageTableView.rowHeight = UITableViewAutomaticDimension".
What Should be common estimate row height.
After reload UITableView content size not getting properly. If user scroll then content size increased.
It is not enough to just set rowHeight to automatic dimension. You also need to install proper constraints for cell subviews so the cell can calculate its height based on that constraints.
To use UITableViewAutomaticDimension for dynamic height calculation, ensure
1 - Leading, Trailing, Top and Bottom constraints to cell are added correctly and not creating any conflicts
2 - Add cell.layoutIfNeeded() in cellForRowAt method of UITableViewDelegate
3 - You can give any height as estimatedRowHeight, 44 is the default value, but you can give 20(your minimum) as well.

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

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.

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.

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