美文网首页
iOS 使用模型动态计算行高

iOS 使用模型动态计算行高

作者: 卉木共纷华 | 来源:发表于2017-07-11 16:18 被阅读48次

    一般情况下,如果cell里面有一个label需要动态计算行高,而且约束简单的话,可以使用系统的cell自适应高度的使用方法。
    添加好自上而下的约束然后使用
    (不添加的greaterThanOrEqual约束也能实现效果,但是约束会红色显示,所以应该是相对于其它label的优先级问题,设置控件间约束,如两个控件间距不是20,可以通过greaterThanOrEqual设为20)

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100; //100是估算高度,根据实际情况写
    

    当cell里面有其它label也需要自适应高度的时候,就不能使用上面的方法,这里我使用模型里预计算cell高度,先在model里面计算cell 里面label高度,再相加获取cell高度,如果cell里面需要计算高度的label在view里,可以在model里写出所有需要计算的view高度,控制器里返回高度的时候返回这些view高度的总和,效果图如下

    红框内的文字

    整理代码放上:

    #import <Foundation/Foundation.h>
    
    #import 
    <UIKit/UIKit.h>
    
    @interface ZYPageDetailCellModel : NSObject
    
    @property (copy, nonatomic) NSString *commentTextString;/*分享文字*/
    @property (copy, nonatomic) NSString *commentTextString1;/*评论1 */
    @property (copy, nonatomic) NSString *commentTextString2;/*评论2 */
     
    /*各 view 高度*/
    @property (assign, nonatomic) CGFloat cellHeight; //除评论父试图外的高度
    @property (assign, nonatomic) CGFloat commentViewHeight; //评论父试图高度
    
    
    @end
    
    
    #import "ZYPageDetailCellModel.h"
    
    @implementation
     ZYPageDetailCellModel
    
    - ( CGFloat )commentViewHeight {
        
        CGSize maxSize1 = CGSizeMake(SCREENWIDTH-70, MAXFLOAT); //label的size,label的宽度,高度随便,不过尽量大,我这里用了最大
    
        /*计算评论内容的高度*/
        //1.评论文字1
        CGFloat textH2 = [self.commentTextString1 boundingRectWithSize:maxSize1 options:NSStringDrawingUsesLineFragmentOrigin         attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.f]} context:nil].size.height;
        
        //2.评论文字2
        CGFloat textH3 = [self.commentTextString2 boundingRectWithSize:maxSize1 options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.f]} context:nil].size.height;
        
        //1.评论父试图
        _commentViewHeight = 10+ 13.5+ 15 + textH2 + 10 + textH3 +10+27+2.5; //从view里第一个控件开始到最后一个之间的高度都要加上
    
        return _commentViewHeight;
    }
    
    - ( CGFloat )cellHeight {
        
        CGSize maxSize = CGSizeMake(SCREENWIDTH-70, MAXFLOAT);
        
        /*计算内容的高度*/
        //1.分享文字
        CGFloat textH1 = [self.commentTextString boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.f]} context:nil].size.height;
        _cellHeight = 47* SCREENWIDTH/320 + textH1 + 56 * SCREENWIDTH/320 + 30;
        
        return _cellHeight;
    }
    
    

    下面控制器里就可以直接使用来计算cell高度了
    这里我自己添加了模型数据

    for (int i=0; i<10; i++) {
            
            ZYPageDetailCellModel *model = [[ZYPageDetailCellModel alloc] init];
            model.commentTextString = @"舒婷说:“我很幸运,生长在这样一个南方岛屿,春夏秋冬,日日夜夜,与绿树鲜花呼吸与共”。鼓浪屿就是这样一个让人着迷的地方。";
            model.commentTextString1 = @"我很幸运,生长在这样一个南方岛屿,春夏秋冬,日日夜夜,与绿树鲜花呼吸与共”。鼓浪屿就是这样一个让人着迷的地方。";
            model.commentTextString2 = @"我很幸运,生长在这样一个南方岛屿,春夏秋冬,日日夜夜,与绿树鲜花呼吸与共”。鼓浪屿就是这样一个让人着迷的地方我很幸运,生长在这样一个南方岛屿,春夏秋冬。";
            [self.dataArray addObject:model];
        }
    //这样就可以把要计算的label文字内容传给model,通过调用model里属性来计算
    
    

    然后在计算行高方法里可以获取:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
        
        ZYPageDetailCellModel *model = self.dataArray[indexPath.section];
        
        return model.cellHeight+ model.commentViewHeight;//model.cellHeight 会计算除评论试图外的高度;model.commentViewHeight 会计算评论试图高度;
    }
    
    

    当然,如果有其他布局,也可以通过这种方法来计算行高,首先分开计算各label所在试图高度,在相加获取最后高度。

    相关文章

      网友评论

          本文标题:iOS 使用模型动态计算行高

          本文链接:https://www.haomeiwen.com/subject/qajmhxtx.html