Can a cell (UITableView) change its height in real time? - ios

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;
}

Related

How to increase UITableView Height Dynamically When number of cell is increased using Objective C?

I am new to iOS. I have implemented a table view which is populated with array values successfully, but I'm not satisfied with the result. When I scroll my table view the top values are hiding.
Here is my problem: based on the cell height the table view height should also get increased dynamically in UITableview. How can I accomplish that?
For UITableViewCell dynamic height you can use following delegate by return UITableViewAutomaticDimension
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 300; //give maximum heigh you want
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
and to increase tableview height based on content size use the below line of code
YOUR_TABLE_VIEW_HEIGHT_CONSTRAINT.constant = [YOUR_TABLE_VIEW contentSize].height;
Hope this will help you

Dynamic cell height issue in UitableView using AutoLayout?

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;
}

Dynamic UIlabel inside UITableViewCell

The user can make a comment which can be can be any size or height. The comment label's dynamic content should fit into the cell.
I have allocated my label inside UITableViewCell with a key value.
cell.CommentLabel.text=[ResDiction objectForKey:#"Comment"];
cell.TimeLabel.text=[ResDiction objectForKey:#"CreateDate"];
cell.UserName.text=[ResDiction objectForKey:#"CreateUserName"];
cell.UserName.adjustsFontSizeToFitWidth=YES;
cell.CommentLabel.adjustsFontSizeToFitWidth=YES;
cell.CommentLabel.numberOfLines = 8;
cell.TimeLabel.adjustsFontSizeToFitWidth=YES;
How do I let the label's content determine the cell height?
You have to use
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableviwCell* cell=[tableview cellForRowAtIndexPath:indexPath];
cell.CommentLabel.text=[ResDiction objectForKey:#"Comment"];
cell.TimeLabel.text=[ResDiction objectForKey:#"CreateDate"];
cell.UserName.text=[ResDiction objectForKey:#"CreateUserName"];
cell.UserName.adjustsFontSizeToFitWidth=YES;
cell.CommentLabel.adjustsFontSizeToFitWidth=YES;
cell.CommentLabel.numberOfLines = 8;
cell.TimeLabel.adjustsFontSizeToFitWidth=YES;
float height= cell.cell.CommentLabel.frame.size.height+TimeLabel.frame.size.height+(all your UIFields you have used)
return height;
}
This method is will return the height for the cell while it is being created.
The code inside will do calculate height of each uiField and combine them, this will work if you are arranging like stacks in UITableview cell, or you have to modify the code inside the function as per you arrangement.

UITableview cell row height issue in ios8

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(!isPopover)
{
if (indexPath.row == selectedIndex)
{
return 122;
}
else
return 60;
}
else
{
return 50;
}
}
same method i used in IoS7. Working fine over there.Help me on this
IOS8 sets the tableViewCell row height, depending on whats in the subView of ContentView. If you have a label in the subView and you want to restrict the lines in the label text then use the noOfLines property to restrict the lines and hence the height of the tableViewCell, similarly if you have buttons or images in the tableView you could size them appropriately, such that tableViewCell will size its height accordingly.

Change Height of a UITableViewCell

I want to make a UITableViewCell bigger. How can I do this, I have tried adjusting its frame but nothing happens. What can I do?
You need to implement the
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
method in your UITableViewDelegate.
You can do like this..
//Change the Height of the Cell [Default is 44]:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (indexPath.section == 0) {
if (indexPath.row == 1) {
return 90;
}
}
return 44;
}
I hope this will help you..
There are 2 steps to change height of a table cell without any additional code:
Select your Table View from your Storyboard. In the Size Inspector change Row Height.
Select your Table View Cell and while it's selected in the Size Inspector change the Row Height.
Note that you MUST set BOTH of Table View's Row Height attribute and Table View Cell's Row Height attribute.
If you just set one of them, it won't work.
I found this answer to be the most informative and most up-to-date to change the height of a table view cell.
TL;DR
Take extra care when making your Auto Layout constraints
Enable row height estimation on your table view in the viewDidLayoutSubviews method.
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 44.0; // set to whatever your "average" cell height is
You can use this code for changing the height of different tableview
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == tableView1)
{
return 55;
}
else if(tableView == tableView2)
{
return 75;
}
}
This may help you.

Resources