首页 > 其他 > 详细

UITableViewCell 高度的自适应

时间:2016-04-25 00:38:45      阅读:231      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!