我需要根据单元格的内容可以动态的修改单元格的高度,已经试了好几种方法,不知道有没有人可以提供能实现的方法?谢谢
你可以试试下面的例子:
- (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;
}
LZ看看我的吧
//计算你需要的新高度
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];
//调整
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;
1.在
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndex:(NSIndexPath *)indexPath
方法中的尾部找到当前Cell中的最后一个元素,根据这个元素在Cell中的坐标,重置cell的frame.size.height。
2.在
-(CGFloat) tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *) indexPath {
UITableViewCell *cell=[self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}