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]; }
  • 相关阅读:
    Ubuntu 16.04 swoole扩展安装注意!!!
    go mod使用指南
    基于gin框架-验证码demo
    go(基于gin开发提升效率)--Air
    go mod路径引入并代码提示
    golang在win10下安装问题(一)
    win10下beego安装注意坑(一)
    API统一管理平台-YApi
    vim编辑
    swool安装(centos7)
  • 原文地址:https://www.cnblogs.com/arenouba/p/5428804.html
Copyright © 2011-2022 走看看