Sorry i know this question has been asked before but i can't seem to get it to work, i would like to change the height of the first cell in my UITableView and keep the rest as default 44. I have implemented the below code (tried various others) and my application builds but the cell height does not change. Any help on this would be great.
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return 150;
}
else {
return 44;
}
}
How did this code work out for you?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0)
return 150;
else
return 44;
}
estimatedHeightForRowAtIndexPath
is used to calculate the position / size of the scroll bar on the side when using autoLayout, not the cell height. You need:
heightForRowAtIndexPath
Related
I've 3-5 dataSource and in ViewDidLoad() I've mentioned the property for dynamic cell height:-
[self.tableView setEstimatedRowHeight:40.0];
[self.tableView setRowHeight:UITableViewAutomaticDimension];
but the tableview don't update accordingly.. first two-three cell shows contents in good way but as soon as I scroll the tableview, cell gets compressed and then cell shows half of the content.
UPDATED
screen of customCell is
tableViewCell
andConstraints Are:tableViewCellConstraint
I have a UIView in cell and label and a uiimageview inside the view
More Update::
I think its coz i'm also using an empty cell to have a space between two cell and in the delegate method for i doing this
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 20.0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row %2 == 0) {
return 15;
}
return UITableViewAutomaticDimension;
}
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;
}
- (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.
Im trying to make some of my cells bigger in height. i tried using
CGRect rect = cell.frame;
NSLog(#"before height: %f",rect.size.height);
rect.size.height +=20;
cell.frame = rect;
NSLog(#"AFTER height: %f",cell.frame.size.height);
in
cellForRowAtIndexPath
and
willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath
*)indexPath
The log shows that the values have changed but it doesnt show any change in the simulator.
Thanks for the help
use tableView:heightForRowAtIndexPath method.
Apple docs clearly explains what to do. UITableView class reference
Every tableView has a delegate property.Set it to your viewController and implement above method. Its signature is
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
So, based on indexPath, return whatever the height you desire.
If you want constant height for all rows, you can use rowHeight property of UITableView.
Use heightForRowAtIndexPath of UITableViewDelegate. Example:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row == _pages.count - 1 ? 408 : 450;
}
To make some cells bigger, you should implement the method tableView:heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (<SOMETHING>) {
return height1;
} else {
return height2;
}
}
tableView:cellForRowAtIndexPath: is for configuring the content of the cell that the tableView needs to display.
Note that tableView:heightForRowAtIndexPath: has performance implications if you have a really large table (1000+ entries), this method is called on every row when the table view displays.
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.