zoukankan      html  css  js  c++  java
  • 动态计算UITableviewcell高度

    转载自:http://www.jianshu.com/p/001fdfe311ae

    作者简书地址:http://www.jianshu.com/users/5dffd76b9caf/latest_articles

    在iOS开发中,我们少不了和UITableview打交道,因为UITableview也是UIKit中最复杂的一个控件了。在使用UITableview的过程中,UITableviewCell也是必不可少的,页面列表形式的展示可谓是各种各样,相信不少童鞋们也曾为复杂的页面布局困惑过,其中比较难的也就数cell的高度自适应了,也就是说cell的高度是根据内容来动态计算的。

    1.不使用Autolayout的时候,计算cell的高度:

    //返回cell的的高度
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        PhotoInfo *photoInfo = [self.dataArr objectAtIndex:indexPath.row];
        [self heightForRowWithModel:photoInfo];
    }
    
    //动态计算cell的高度 
    - (CGFloat)heightForRowWithModel:(PhotoInfo *)photoInfo 
    { 
        //这里只写了label的计算
          //文本的高度 
        CGSize texSize = [self labelAutoCalculateRectWith:photoInfo.instruction FontSize:15 MaxSize:CGSizeMake(200,1000)];
        //3.返回cell 的总高度 
        return 44 + textSize.height;
    }  
    
    /*根据传过来的文字内容、字体大小、宽度和最大尺寸动态计算文字所占用的size
                  * text 文本内容 
                  * fontSize 字体大小
                  * maxSize  size(宽度,1000)
                  * return  size (计算的size)
                  */
    - (CGSize)labelAutoCalculateRectWith:(NSString*)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize
    {
        NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];
       paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
        NSDictionary* attributes =@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSParagraphStyleAttributeName:paragraphStyle.copy};
        CGSize labelSize;
          //如果是IOS6.0
        if (![text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]){
            labelSize = [text sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
        }
        //如果系统为iOS7.0
        else
      {
              // iOS7中用以下方法替代过时的iOS6中的sizeWithFont:constrainedToSize:lineBreakMode:方法
            labelSize = [text boundingRectWithSize: maxSize
                                           options: NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine
                                        attributes:attributes
                                           context:nil].size;
        }
        labelSize.height=ceil(labelSize.height);    
        labelSize.width=ceil(labelSize.width);
        return labelSize;
    }

    2.使用Autolayout,- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize这个方法就能搞定。不过,首先要在Xib上布局cell。


    Mou icon
    //返回cell高度
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        SystemMSGTableViewCell *cell = self.cell;
        SystemmsgInfo *data = self.dataArr[indexPath.row];
        CGFloat height = [cell heightForCell:data];
        return height;
    } 
    
    //动态计算cell的高度
    - (CGFloat)heightForCell:(SystemmsgInfo *)data
    {
        self.widthLabel.constant = ScreenWidth - 40;
        self.contentLabel.text = data.promotionInfo;
        CGSize size = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        NSLog(@"h=%f", size.height + 1);
        return 1  + size.height;
    }

    效果图:


    Mou icon



    文/Jingege(简书作者)
    原文链接:http://www.jianshu.com/p/001fdfe311ae
    著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
  • 相关阅读:
    SpringBoot启动里的细节问题
    SpringBoot运行过程从SpringApplication开始解读
    SpringBoot应用启动流程
    SpringBoot上传相关配置
    planUML时序图
    JAVA设计模式--单例模式
    中国云运营商横向对比——IaaS服务对标
    ansible api2.0 多进程执行不同的playbook
    自动化运维平台搭建
    django-将数据库数据转换成JSON格式(ORM和SQL两种情况)
  • 原文地址:https://www.cnblogs.com/CodingMann/p/5511890.html
Copyright © 2011-2022 走看看