I have a tableviewcell with two labels and the label's height will change depending on the content so tableviewcell height will automatically change.
now how can I make tableview cell height to autogrow ?
Please clarify me if the tableview cell height will autogrow if the label height changes ?
To make it tableview cell autogrow you have to keep in mind of the following two things in mind,
Right constraint(top, bottom, right & left) for the UI elements inside the tableview cell.
Usage of UITableViewAutomaticDimension & estimatedRowHeight property as follows,
tableView.estimatedRowHeight = 100 // A rough estimated height for your cell.
tableView.rowHeight = UITableViewAutomaticDimension
Refer the following tutorial for better indepth explanation on how to implement it.
http://matteomanferdini.com/the-correct-way-to-display-lists-in-ios-and-what-many-developers-do-wrong/#autolayout
https://www.youtube.com/watch?v=e1KJQs0frbM
Related
I am trying to make a custom TableViewCell for a tableview, and the size is 414x350. The problem I am having is when table view get loaded all the sizes are squeezed and not right. I have tried all of the followings:
Assigning row height from code
tableView.estimatedRowHeight = 350
tableView.rowHeight = UITableView.automaticDimension
Assigning row height from storyboard in tableview window
Assigning row height from storyboard in tableviewcell window
The result is still the same. Any help is appreciated!
For any cell's automatic row dimension to be calculated properly, in your Storyboard you must have a direct line of vertical constraints between every item that goes all the way from the top to the bottom. If you set the constraints yourself, same thing applies.
Don't assign any row heights, except the estimated.
I am using table view for my chat application, in which the chat responses are added either one of two textviews as shown in the image. The thing is whenever I add a large text, the cell height does not expand to show the full text. It only shows the last line of that text. I am using objective-c. Is there a way to solve this ?. I do not want to completely create a new view like the questions asked before, I want changes to this specific view.
First, you need to use Label and set top and bottom constraint between label and contentView cell.
In order to allow the self-sizing cell mechanism to work, you must set the rowHeight property on the table view to the constant UITableViewAutomaticDimension. Then, you simply need to enable row height estimation by setting the table view's estimatedRowHeight property to a nonzero value, for example:
_tableView.estimatedRowHeight = 44.0; // set to whatever your "average" cell height is
_tableView.rowHeight = UITableViewAutomaticDimension;
You need to defined self-sizing cell like this.
tableCountries.estimatedRowHeight = 100.0;
tableCountries.rowHeight = UITableViewAutomaticDimension;
Make sure you have a proper constraint. You can have a constraint like this.
In the below label you need to set your textView top position. That's why when the below label expands it automatically expand label and text view will push to bottom.
I think your cell is not gettings its height because of textview scrolling maybe that will help:-
Use UITableViewAutomaticDimension and for that your tableview cell content should justify both top-bottom leading and trailing constraints to calculate cell height.
Set scrollEnabled to "false" for your textview.
With everything on the place your cell will get its height for sure.
you can do it in many ways :
1) You can set dynamic height of cell using UITableViewAutomaticDimension.
You need to set proper constraints in storyboard like setting top and bottom.
in ViewDidLoad add below :
tblView.rowHeight = UITableViewAutomaticDimension
tblView.estimatedRowHeight = 100
and in tableView cell's height method :
return UITableViewAutomaticDimension
2) Find text's height dynamically using below function :
let width = 100.0 \\your text label's width you gave
var height = chatMsg.text?.height(withConstrainedWidth: width, font: chatMsg.font!)
using above code you can find the height of your text. and you can use it dynamically
3) there are many code available to use for chat application in which you just need to pass text/image, and it will handle everything else.
Link : https://github.com/kerry/iMessageBubble
Add these two delegate methods:
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
Create Custom UITableViewCell with Three UILabel like lbl1,lbl2,lbl3.
Add the AutoLayout Constraints to lbl1 left, right, top and bottom. With fixed height (30).
Then set same constraints to remaining two Labels.
Here is main things:
1.select lbl1 heigh and double click on height constraint and set >=30
Do same things with lbl2 and lbl3.
Final step is:
Select lbl2 top constraints, change the constraints property like <=8.
Do same things with lbl3, that's it we are done all.
thanks..
I have tableview inside tableview cell , I am using UITableViewAutomaticDimension in heightForRowAt, tableview height is not adjusting automatically. I have to calculate cell height manually.
Any idea ?
You are missing either bottom or top constraint.
Consider you have to add label in table cell, then you have give both top and bottom constraint to label (make line number 0).
It will automatically increase the size of table cell according to label text.
Don't forgot to set estimatedRowHeight.
To get automatic cell height work correctly you must hook all your constraints properly from top to bottom in the cell Xib , or if you're creating constraints in code , that's how auto-layout can calculate the height automatically
self.tableView.estimatedRowHeight = 50.0
self.tableView.rowHeight = UITableViewAutomaticDimension
Set all constraint like bottom, top, left, right.
I have a screen that contains 2 levels of nested UITableViews.
Each cell of the outer (top level) TableView is a UITableViewCell That contains a UITableView.
The TableView inside each cell is drawn correctly, all constraints implemented properly UITableViewAutomaticDimension is performing correctly.
The problem is with the top level TableView cells height, Im trying to set their height with:
table.estimatedRowHeight = 400
table.rowHeight = UITableViewAutomaticDimension
and also return UITableViewAutomaticDimension in heightForRow.
Cells get height of 45 when their height is 400 or so (dynamic height for each cell).
What am I missing here?
Could it be that the top level TableView just cant estimate the inner cell height?
Help would be much appreciated
Have a look at this Tableview Inside Cell . It contains table view inside cell that is completely constructed using auto layout. Hope this may help.
I have a UITableView with cells that have inside a UICollectionView.
Now all works great except of UICollectionViewCell scrolling i want that the height of table view cell follow the Collection view cell content.
In simple word i want scrolling in table view but not inside single cell i need to display all contents of UIcollectionViewCell without scrolling inside it.
Thanks
You have to calculate the collectionviews' heights and set a height constraint on each of them. then add top and bottom constraints from the collectionviews to their superviews (the tableviewcells / their contentviews) and let autolayout do its work.
don't forget
tableView.estimatedRowHeight = 44 // or something similar
tableView.rowHeight = UITableViewAutomaticDimension
in the controller's viewDidLoad to make the automatic row height calculation work.