我有一个具有自定义 UITableViewCells 的 UITableView,这是来自用户的评论。现在,我有 115.0f 高的单元格,但我希望根据评论的长度来改变高度。如果评论超过三行,我希望用户能够选择单元格,并且单元格展开以显示整个评论。我一直在使用该[UIView animateWithDuration: completion:
方法来扩展单元格,但是我不知道如何根据文本的长度来确定单元格的正确大小。有人可以帮我吗?这是一些代码:
- (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
}
}