zoukankan      html  css  js  c++  java
  • UITableViewCell 高度的自适应

    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]; }
  • 相关阅读:
    【css基础修炼之路】— 谈谈元素的垂直水平居中
    git在linux安装步骤详解!!
    idea :不支持发行版本11问题
    centos7 升级gcc
    mysql--优化
    Docker安装
    使用idea从零编写SpringCloud项目-zuul
    使用idea从零编写SpringCloud项目-Hystrix
    使用idea从零编写SpringCloud项目-Feign
    使用idea从零编写SpringCloud项目-Ribbo
  • 原文地址:https://www.cnblogs.com/arenouba/p/5428804.html
Copyright © 2011-2022 走看看