I'm having a UITableView. In that I have a single label. I am doing following for label:
Set number of lines to zero
Set top space to container margin
Set bottom space to container margin
My contents for UILabel may vary, some time it may have 3 lines, some times 10 lines etc. My problems is it displays only 2 rows. How to adjust the height of cell so that UILabel will show the whole contents.
Thanks in advance.
You should set numberoflines to 0. Provide either leading & trailing space constraints for your label or fixed width constraint along with top & bottom space constraints. Then use tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
set your label's constraint with cell like top bottom leading and trailing, set number of line for labels to zero and line break mode to word wrap. now place this two tableView methods to calculate cell height dynamically.
For Swift 3.0
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
for Objective C
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
so it will take cell size dynamically as you want.
hope this will help you.
Related
I would like to adjust the number of lines in a UILabel dynamically. For example, there could be a long string or a short string. If there are less than 10 characters, there should be one line. If there are 10-20 characters, it should be two lines. This UILabel is inside of a UITableViewCell. Currently, if there is a long string, then the UILabel just shows "..." wherever it runs out of space, so the string is cut off. How can I prevent this from happening?
Try:
nameOfLabel.numberOfLines = 0
0 is supposed to be dynamic.
Constaints from Top Left Right Buttom of label and label.numberOfLine = 0 make it dynamically resizable according to dynamic content.
First set numberOfLines to 0 for that UILabel from Storyboard
Than set estimated height for your UITableView
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 108.0
Than Add heightForRowAt method
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Hope it Help!
Assign and implement tableview dataSource and delegate
Assign UITableViewAutomaticDimension to rowHeight & estimatedRowHeight
Implement delegate/dataSource methods (i.e. heightForRowAt and return a value UITableViewAutomaticDimension to it)
-
Objective C:
// in ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property IBOutlet UITableView * table;
#end
// in ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.table.dataSource = self;
self.table.delegate = self;
self.table.rowHeight = UITableViewAutomaticDimension;
self.table.estimatedRowHeight = UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
Swift:
#IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Don't forget to set dataSource and delegate for table
table.dataSource = self
table.delegate = self
// Set automatic dimensions for row height
table.rowHeight = UITableViewAutomaticDimension
table.estimatedRowHeight = UITableViewAutomaticDimension
}
// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
For label instance in UITableviewCell
Set number of lines = 0 (& line break mode = truncate tail)
Set all constraints (top, bottom, right left) with respect to its superview/ cell container.
Optional: Set minimum height for label, if you want minimum vertical area covered by label, even if there is no data.
Note: If you've more than one labels (UIElements) with dynamic length, which should be adjusted according to its content size: Adjust 'Content Hugging and Compression Resistance Priority` for labels which you want to expand/compress with higher priority.
You need to give constraints to tableview cell's label leading, trailing, top and bottom like below...
Also set label's number of lines to zero and line breaking mode to word wrap.
And in your main view controller just add this line...
self.tableView.estimatedRowHeight = UITableViewAutomaticDimension
The above line automatically calculates the cell height as per its content.
Hello I have a UITableView with x# of cells. the last cell I have two UILabels. When the second UILabel text is set I am trying to get the cell and the UILabel to resize to show the text.
Here is what I have:
The UILabel - LabelBio (orange) has:
Lines: 0
Baseline: Align Baselines
Line Break: Word Wrap
Autoshrink: Fixed Font Size
The constraints for ContentView, LabelSellerInfo and LabelBio are set as follows:
LabelSellerInfo
LabelBio
ContentView
With those settings here is what a get:
I have tried many variations but cannot seem to get the Bio label to grow and shrink, sometimes if I get the label to grow the cell is still too small.
Can anyone help me to understand what I am doing wrong and show me how to get the constraints correct to get this to work?
Thank you
Set tableView.rowHeight = UITableViewAutomaticDimension
Remove this function in your code func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
Set height constraint of LabelBio greaterOrEqualThan 0
here are the steps ...
hope you know about autylayout. use constraints (left to content view, right to content view, bottom to content view and verticle to your first label)
then in your viewdidLoad method add YourTableView.estimatedRowHeight = 200 -> give any height you want.
finally define below method :
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
you good to go.
I need to set UITableViewCell dynamic height based on it's content.
Here is my demo image:
In this to label which have dynamic content so based on that label content my cell height should change..
I get the constraint error. In this cell two UILabel is available and both have dynamic content size. (I already make the demo with auto resizing cell with only one label.) But in this cell two label are there. So error is xcode suggest me to give Y POS or Height to label, but I already set top constraints. (4 side constraints with label line set to 0 Zero) But still it's fails.
I also try with this code
self.tableView.estimatedRowHeight = 250.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
Here is my demo code
Look at the screenshot of my output. For both the labels , you should give all the four constrains and you have to change the heights of both the labels priority to 750 & set the hugging and resistance priority to 1000. Then you have to change the number of lines to 0 in storyboard
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 250
}
Add this code into viewDidLoad() method:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
In Swift 3, Use
func tableView(_ tableView: UITableView,estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return HEIGHT // Maximum cell height
}
In table view cell, Set dynamic height constraint to the text view (Greater than or equal) - Maximum text view height
I found the problem. After setting text to UILabel, it doesn't still set correct height frame to its content. In cellForRowAt, just add this:
//these make height frame both of labels became correct
cell.lbName.sizeToFit()
cell.lbAge.sizeToFit()
// update cell layout
cell.layoutIfNeeded()
Have a look at the UITableViewDelegate function tableView(_:heightForRowAt:). It’ll work in your case if you are able to calculate the height for a given index path.
As you can see in this picture
, the imageView's size is fixed and I want to have the cell's subviews "exert pressure" and make the content view expand to fit them. Let the intrinsic content size of these subviews drive the height of the table view cell's content view. It's obvious that the cell height depends on the maximum height of messageButton and imageView:
How should I add those constraints?
maybe you can try this:
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
but before that, you should use Auto-layout to add constraints correctly, you don't need think about the imageView and the button height, just add the constraints of distance to the bottom and top. Good luck!
In Swift 3
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
I have a custom cell:
But when tableView loads i've got overlaping cells in it:
I've tried to insert the following code into my viewDidLoad():
tableView.estimatedRowHeight = tableView.rowHeight
tableView.rowHeight = UITableViewAutomaticDimension
But it does'not helps.
From the docs:
//Declaration
//SWIFT
optional func tableView(_ tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
//OBJECTIVE-C
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
Make the method return the height of the cells. If this does not help, then you need to give us more details (and constraints probably).
EDIT:
I managed to fix the issue by doing 2 things:
Change those two lines:
tableView.estimatedRowHeight = 150
tableView.rowHeight = UITableViewAutomaticDimension
The tableview will automatically make the cells smaller
In the cell xib, you should add the constraint to the bottom of the cell. This way the height can be properly calculated.
If you want I can send you a working copy of the project, just give some contact mail or sth.
When you do this:
tableView.estimatedRowHeight = tableView.rowHeight
Assuming that tableView is a UITableView, here you need to give a number, not the UITableView rowHeight property (something like 100).
Here you give an estimated height just to let the phone calculate the total height of the table, it is not the real height of each cell. So give the average heigh you estimate.
The tableView.rowHeight = UITableViewAutomaticDimension is ok.
And also review the constraints in the cell view. To get this work you have to have constraints from the top to the bottom, and in the middle of the labels.
Edit: I've seen that the last constraint is missing (a bottom constraint for Date label)
Now, seeing the code, I'll say that this method is not neccesary:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Hope it helps.