自定义cell自适应高度

时间:2023-03-08 23:44:02
自定义cell自适应高度

UITableView在许多App种被大量的应用着,呈现出现的效果也是多种多样的,不能局限于系统的一种样式,所以需要自定义cell

自定义cell呈现的内容也是多种多样的,内容有多有少,所以需要一种能让自定义cell自适应高度的方法

下面举例的是让自定义cell中的UILabel能够根据文字内容的多少自适应高度

//求一段文本的显示高度
+ (CGFloat)heightForString:(NSString *)string { //字典dic中存的是UILabel中显示文字的字体信息
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:],NSFontAttributeName, nil];

CGRect rect = [string boundingRectWithSize:CGSizeMake(KimageWidth * , ) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil]; return rect.size.height; }
//返回cell的高度
+ (CGFloat)cellHeightForStudent:(Student *)student {
CGFloat totalHeight = + [GirlTableViewCell heightForString:student.introduce];
return totalHeight > ? totalHeight :;
}

下面是效果

自定义cell自适应高度