ユーザーからのコメントであるカスタム UITableViewCells を持つ UITableView があります。現在、セルの高さは 115.0f ですが、コメントの長さに基づいて高さを変更したいと考えています。コメントが 3 行を超える場合、ユーザーがセルを選択できるようにし、セルを展開してコメント全体を表示できるようにします。メソッドを使用し[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
}
}