有時我們使用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;
}