I need to resize the cell height based on the content size/length.tried several methods, which one gives the exact height without overlapping?
-
1check my answer given here http://stackoverflow.com/a/12600584/1538079 – Niru Mukund Shah Nov 02 '12 at 06:43
-
Do you got the answer which is useful to implement? – Ramz Nov 02 '12 at 07:02
-
yup..Customizing UITableViewCell's height.. this helped me – DD_ Nov 02 '12 at 07:05
-
-(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; } – DD_ Nov 02 '12 at 07:06
-
@RamkumarThiyyakat check the post by Foram Mukund Shah – DD_ Nov 02 '12 at 07:07
-
Your questions meant that you wants to set tableview cell height dynamically. – Ramz Nov 02 '12 at 07:11
-
@Deepak i think you want to change the UITableViewCell Dynamically, m i right?? – Paras Joshi Nov 02 '12 at 07:23
-
http://stackoverflow.com/a/5103517/1059705 http://stackoverflow.com/q/8030608/1059705 – Bala Nov 02 '12 at 12:07
5 Answers
see this tutorial for change UITableViewCell
Height Dynamically..
and also use this tutorial..
uitableviewcell-dynamic-height
also use tableView:heightForRowAtIndexPath: method for set height of cell with indexpath

- 20,427
- 11
- 57
- 70
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;
}
-
-
-
That was the answer for his question. Feedback is an object and it stores the value from an array. – Guru Nov 04 '12 at 17:10
-
hey,What is feedback? Is it a class of urs? may i know what all you do in that particular class – DD_ Nov 05 '12 at 03:34
Sample you can edit and try
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *itemAtIndex = (NSDictionary *)[chatQuestions objectAtIndex:indexPath.row];
if ([self sizeForText:[itemAtIndex objectForKey:@"text"]].height+20>50) {
return [self sizeForText:[itemAtIndex objectForKey:@"text"]].height+20;
}
else{
return 50;}
}
-(CGSize)sizeForText:(NSString*)text
{
CGSize constraintSize;
constraintSize.width = 190.0f;
constraintSize.height = MAXFLOAT;
UIFont *labelFont = [UIFont fontWithName:@"Noteworthy-Light" size:18];
CGSize stringSize =[text sizeWithFont:labelFont constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
return stringSize;
}

- 596
- 1
- 6
- 23
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;

- 6,401
- 3
- 33
- 39
In your case you need to set the height of cell based on the label.
Check the link :
http://dcraziee.wordpress.com/2013/05/22/calculate-size-of-uillabel-base-on-text-in/
There is a function named
-(CGFloat)getHeightForLabel:(NSString *)_str font:(UIFont *)fontOfObject
Use that function to calculate height as :
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat _height = [self getHeightForLabel:self.label.text font:[self.label font]];
return _height;
}

- 178
- 7