有时我们使用tableviewcell展示数据时,接受到的数据会超出我们初始化时设定的cell高度,这时我们就需要自适应cell的高度了.下面是返回cell高度的代码
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
luckNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 40, kScreenWidth - 90, 40)];
NSString *str = [[dataArray[indexPath.row] objectForKey:@"numbers"] componentsJoinedByString:@","];
luckNumberLabel.text = [NSString stringWithFormat:@"您的幸运号: %@",str];
luckNumberLabel.font = FONT(13.0);
luckNumberLabel.lineBreakMode = NSLineBreakByCharWrapping;
luckNumberLabel.numberOfLines = 0;
//自适应高度
CGRect txtFrame = luckNumberLabel.frame;
luckNumberLabel.frame = CGRectMake(80, 55, kScreenWidth - 90, txtFrame.size.height = [luckNumberLabel.text boundingRectWithSize:CGSizeMake(txtFrame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:luckNumberLabel.font,NSFontAttributeName, nil] context:nil].size.height);
luckNumberLabel.frame = CGRectMake(80, 40, kScreenWidth - 90, txtFrame.size.height);
return luckNumberLabel.maxY + 50;
}