I have to calculate dynamic height of cell depends on two label, left label and right label. left label can have empty string . I want to give a specific fixed height if left label is empty else it will take left label height. Cant able to give fixed height when label is empty
This code is not working
self.tableView.estimatedRowHeight = 50
Your constraint for your both label should be,
top,bottom,leading and trailing.
and set below code in your viewDidload
self.tableView.estimatedRowHeight = 50
self.tableView.rowHeight = UITableViewAutomaticDimension;
Set constraints from top and bottom of the cell to both labels.
Then add the following code in your view controller
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
Related
I'm having trouble with reducing the spacing between my cells, I want it to be 1 px. Inside my cell I have a stackView which fills my entire cell, both set to 70.
I can accomplish almost what I want by adding negative constrains, but then my cells gets too big, I only want it to be 70. I set the cell to 70 in the custom cell class
self.frame.size.height = 70
And my stackView is set to the very same. I don't see why I get that amount of spacing between my cells?
Please use the below method to adjust the cell height;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 78;
}
1) Between 2 cells of table view always has no space
2) You set self.frame.size.height = 70 in your custom cell. But it will be useless ! Try this datasource method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
3) Please check the height of the white view
I have a cell with a label, sometimes the label text changes and the cell should be expanded a little bit. I'm using auto layout and variable height cells (you know, UITableViewAutomaticDimension and estimatedRowHeight). It seems it is not working unless after modifying the label I call reloadData or reload that specific cell.
Is that normal? Is there any way to do this automatically or without having to use the view controller to reload the cell that now should have another height? Thanks.
I had a similar problem in one of my apps and what I did was
add 2 constraints, let d be the default height of the label
1. height of the label >= d, priority = 750
2. height = d, priority = 1000
and uitableview cell attribute to UITableViewAutomaticDimension
This worked for me.
try this one
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[[arrFeed objectAtIndex:indexPath.section] objectForKey:#"post_keywords"] isEqualToString:#""])
{
int heigthHeading = [self getHeightForText:[[arrFeed objectAtIndex:indexPath.section] objectForKey:#"post_heading"] withFont:[UIFont systemFontOfSize:17.0] andWidth:245.0];
return 25+heigthHeading;
}
else
{
int heigthHeading = [self getHeightForText:[[arrFeed objectAtIndex:indexPath.section] objectForKey:#"post_heading"] withFont:[UIFont systemFontOfSize:17.0] andWidth:245.0];
return 25+heigthHeading;
}
}
Make cell height dynamic (You can also add multiple label on same cell like this way And your cell increased height depends on labels text)
Add Four Constraints to lable (Top, Bottom, Leading, Trailling)
Set Number Of line 0 and line break mode word wrap
Add this two delegate method
-(CGFloat)tableView:(UITableView )tableView estimatedHeightForRowAtIndexPath:(NSIndexPath )indexPath
{
return 44;
}
-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
return UITableViewAutomaticDimension;
}
Is it possible to use this feature without using text related views? This would be to not have to calculate the height, but instead let the cell self-size using subviews with constant height constraints, and eventually changing those height constraints at runtime.
The proper way to handle dynamic cell height changes in a table view is through tableView:heightForRowAtIndexPath: in UITableViewDelegate. You can grab the height from the subviews but you have to link that value to the index path of the cell.
Yes,you could build a tableviewcell with dynamic height.
1.use constraints to configure your tableviewcell's subviews
2.add a line of code In viewdidload method:
self.tableView.rowHeight = UITableViewAutomaticDimension
3.in some cases you must add another line of code In viewdidload method:
self.tableView.estimatedRowHeight = 40 //or any value other than your tableviewcell's height in IB.
Yes you can do that.
First Add constraints to subviews and add the following code in .m file.
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
I have a tableview UITableViewAutomaticDimension for auto layout, and I get the tableview content size to make the tableview height as it content height after tableview cell configure and reload, this height isn't right it seems calculate by estimatedRowHeight. I know I can get the last right height use kvo to observe the tableview content size, but I don't know which is the last right one, because I just want do something once when content size is right. How can I do that?
you need to add a delegate method like
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
before that you should pin 4 sides on that contents which you dropped in prototype cell.
now you got the exact row heights of each cells.
I met this issue after I upgraded xcode to 6.3. The former build compiled using xcode 6.2 doesn't have this issue and works great.
The issue is: After switch to other tab and back, or back from push segue, the width of most cells will shrink a little bit. But some don't. And it looks like following
The label content in 2nd line of cell has the correct width. And all cells display content with the correct width after first launch.
In above image, there are two uitableviewcells.
content view : blue;
background view: light green;
content Label: purple;
The bg view and contentLabel in 1st cell shrinked. That's what I don't want.
I use storyboard to set the constraints.
bg view's constraints.
labelview's constraints
I also tried to cache the cell height as per this question UITableView layout messing up on push segue and return. (iOS 8, Xcode beta 5, Swift), but it doesn't work.
//In viewDidLoad
self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.contentInset = UIEdgeInsetsZero;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
float height = [[self.estimatedRowHeightCache objectForKey:[NSString stringWithFormat:#"%ld", indexPath.row]] floatValue];
if (height) {
return height;
} else {
return UITableViewAutomaticDimension;
}
}
//cache row height. I observed this doesn't been called in first launching in debugging.
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[self.estimatedRowHeightCache setValue:[NSNumber numberWithFloat:cell.frame.size.height] forKey:[NSString stringWithFormat:#"%ld", indexPath.row]];
}
Thanks for your help!
Well you are not setting the Horizontal Space Constraint for the view on top of your contentView. You are only setting Horizontal Space Constraint for your label and its superview.
Do this
Select the view which contains the label
Editor > Pin > Leading Space to SuperView
Implement DataSource Method for Tableview WillDisplayCell
Or you can implement in cell class (if it's custom cell) method - (void)awakeFromNib and there set cell frame.