I have a UITableView that has custom UITableViewCells, which are comments from the users. Right now, I have the cells 115.0f high, but I want the height to changed based on how long the comment is. If the comment is more than three lines, I want the user to be able to select the cell, and the cell to expand to show the whole comment. I have been using the [UIView animateWithDuration: completion:
method to expand the cell, but I don't know how to figure out the correct size the cell should be based on how long the text is. Can someone help me out? Here is some code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
return 480;
}
else
{
if (self.cellType == 0)
{
return 115;
}
else
{
return 75;
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row > 0)
{
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
if ([cell isKindOfClass:[CommentCell class]])
{
CommentCell *cell = (CommentCell *)[tableView cellForRowAtIndexPath:indexPath];
UILabel *label = cell.commentBodyLabel;
NSString *theText = label.text;
CGSize constraintSize = CGSizeMake(label.frame.size.width, label.frame.size.height);
CGSize labelSize = [theText sizeWithFont:label.font constrainedToSize:constraintSize lineBreakMode:label.lineBreakMode];
CGFloat labelHeight = labelSize.height;
int numLines = (int)(labelHeight/label.font.leading);
NSLog(@"there are %i lines", numLines);
NSLog(@"The text height is %f", labelHeight);
if (numLines == 3)
{
//This is where I should expand the cell
}
}