一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

ios开发之UItableViewCell自适应高度

时间:2022-06-25 23:36:17 编辑:袖梨 来源:一聚教程网

有时我们使用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;
 
 
}

热门栏目