自定义cell自适应高度

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

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

 

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

//求一段文本的显示高度
+ (CGFloat)heightForString:(NSString *)string {

//字典dic中存的是UILabel中显示文字的字体信息 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
CGRect rect = [string boundingRectWithSize:CGSizeMake(KimageWidth * 3, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil]; return rect.size.height; }
//返回cell的高度
+ (CGFloat)cellHeightForStudent:(Student *)student {
    CGFloat totalHeight = 65 + [GirlTableViewCell heightForString:student.introduce];
    return totalHeight > 120 ? totalHeight :120;
}

下面是效果

posted @ 2016-02-22 14:08  _Mo  阅读(219)  评论(0编辑  收藏  举报