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]; }
  • 相关阅读:
    vmware下玩ubuntu总结
    .Net Json 字典序列化
    Flex Air TitleWindow 拖动范围控制
    TimesTen 问题荟萃
    TimesTen 时间戳(timestamp)用法
    批量数据插入 (.Net, ODBC)
    腾讯 360浏览器 调用js问题
    [转]Android项目源码混淆问题解决方法
    Intent调用大全
    View实现涂鸦、撤销以及重做功能【转】
  • 原文地址:https://www.cnblogs.com/arenouba/p/5428804.html
Copyright © 2011-2022 走看看