码迷,mamicode.com
首页 > 其他好文 > 详细

UITableViewCell 高度的自适应

时间:2016-04-25 00:40:24      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

1.在 自定义的 cell 类 MyTableViewCell 中 声明两个类方法  根据文本的高度 计算cell 的高度

//两个类方法:文本和cell的自适应

+(CGFloat)heightForString:(NSString *)string;
+(CGFloat)cellHeightForStudent:(Student *)student;

2.实现这两个方法

//在类方法中,不能使用实例变量

+(CGFloat)heightForString:(NSString *)string{

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];

//1000 代表的是在这么多高度下计算文本的 高度 文本内容的宽度通常是 label的宽
    CGRect rect = [string boundingRectWithSize:CGSizeMake(“文本内容的宽度", 1000)  options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

    return rect.size.height; //返回的值是文本的高度

}//返回的文本字符串高度供cell使用

//根据模型中的文本内容计算高度
+(CGFloat)cellHeightForStudent:(Student *)student{

    return 10 + [TableViewCell heightForString:student.introduce] > 140 ? 10 + [TableViewCell heightForString:student.introduce] : 140;
}

3.在控制器中 加上这两句代码

- (void)viewDidLoad
{


  
//自适应高度

    self.tabVC.rowHeight = UITableViewAutomaticDimension;


    self.tabVC.estimatedRowHeight = 10000;


}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ //先取模型 Student *student = _dataArray[indexPath.row]; return [TableViewCell cellHeightForStudent:student]; }

 

UITableViewCell 高度的自适应

标签:

原文地址:http://www.cnblogs.com/arenouba/p/5428804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!