Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change UITable cell height?

I need to resize the cell height based on the content size/length.tried several methods, which one gives the exact height without overlapping?

like image 226
DD_ Avatar asked Jan 29 '26 18:01

DD_


2 Answers

see this tutorial for change UITableViewCell Height Dynamically..

Resizing-A-UITableViewCell

and also use this tutorial..

uitableviewcell-dynamic-height

also use tableView:heightForRowAtIndexPath: method for set height of cell with indexpath

like image 162
Paras Joshi Avatar answered Feb 01 '26 13:02

Paras Joshi


This is from my previous answer - Customizing UITableViewCell's height:

Use this method to get the text height of the text

-(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth
{

CGSize maximumSize = CGSizeMake(labelWidth, 10000);

//provide appropriate font and font size
CGSize labelHeighSize = [text sizeWithFont: [UIFont fontWithName:@"Trebuchet MS" size:13.0f]
                         constrainedToSize:maximumSize
                             lineBreakMode:UILineBreakModeTailTruncation];
return labelHeighSize.height;
}

This method will return the height of the text you are passing. Add this method in your class. And use the tableView:heightForRowAtIndexPath: delegate method to set the height for each cell

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   Feedback *item = [self.items objectAtIndex:indexPath.row];

   CGFloat textHeight = [self getLabelHeightForText:item.comment andWidth:162];//give your label width here
    return textHeight;
}    
like image 35
Guru Avatar answered Feb 01 '26 14:02

Guru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!