分享

自定义cell中自适应高度

 叹落花 2015-01-10
cell的.h中

// 重写模型setter方法

- (void)setTextModel:(TextModel *)textModel

{

    if (_textModel != textModel) {

        [_textModel release];

        _textModel = [textModel retain];

        

        // 头像

        NSString *fileURL = textModel.user[@"avatar_url"];

        NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];

        _userImageView.image = [UIImage imageWithData:data];

        // 用户名

        _userNameLabel.text = textModel.user[@"name"];

        // 文字

        _contextLabel.text = textModel.content;

        // 点赞数

        if (textModel.digg_count >= 10000) {

            _diggLabel.text  = [NSString stringWithFormat:@"%0.1f万",(textModel.digg_count%1000)/10.0];

        }else{

            _diggLabel.text  = [NSString stringWithFormat:@"%ld",textModel.digg_count];

        }

        // 踩数

        if (textModel.bury_count >= 10000) {

            _buryLabel.text = [NSString stringWithFormat:@"%.1f万",(textModel.bury_count%1000)/10.0];

        }else{

            _buryLabel.text = [NSString stringWithFormat:@"%ld",textModel.bury_count];

        }

        // 评论数

        if (textModel.comment_count >= 10000) {

            _commentLabel.text = [NSString stringWithFormat:@"%.1f万",(textModel.comment_count%1000)/10.0];

        }else{

             _commentLabel.text = [NSString stringWithFormat:@"%ld",textModel.comment_count];

        }

        

        

       

        

        

        // 计算文字高度

        CGFloat height = [TextTableViewCell calsLabelHeightWithTextModel:textModel];

        CGFloat height2 = height + 70;

        

        // 将文字Label高度修改

        CGRect frame = _contextLabel.frame;

        frame.size.height = height;

        _contextLabel.frame = frame;

        

        // 将底层背景高度修改

        CGRect frame2 = _bigImageView.frame;

        frame2.size.height = height+80;

        

        _bigImageView.frame = frame2;

        // 点赞

        CGRect frame3 = _diggButton.frame;

       // frame3.origin.y = height + 10;

        frame3.origin.y = height2 - 21;

        _diggButton.frame = frame3;

        

        CGRect frame4 = _diggLabel.frame;

        frame4.origin.y = height2 - 24;

        _diggLabel.frame = frame4;

        

        // 踩

        CGRect frame5 = _buryButton.frame;

        frame5.origin.y = height2 - 20;

        _buryButton.frame = frame5;

        

        CGRect frame6 = _buryLabel.frame;

        frame6.origin.y = height2 - 24;

        _buryLabel.frame = frame6;

        

        // 评论

        CGRect frame7 = _commentButton.frame;

        frame7.origin.y = height2 - 20;

        _commentButton.frame = frame7;

        

        CGRect frame8 = _commentLabel.frame;

        frame8.origin.y = height2 - 24;

        _commentLabel.frame = frame8;

        

        // 分享

        CGRect frame9 = _shareButton.frame;

        frame9.origin.y = height2 - 20;

        _shareButton.frame = frame9;

        

        // 竖线

        CGRect frame10 = _lineVIew1.frame;

        frame10.origin.y = height2 - 20;

        _lineVIew1.frame = frame10;

        

        CGRect frame11 = _lineview2.frame;

        frame11.origin.y = height2 - 20;

        _lineview2.frame = frame11;

        

        CGRect frame12 = _lineView3.frame;

        frame12.origin.y = height2 - 20;

        _lineView3.frame = frame12;

    }

}


// 文字高度

+ (CGFloat)calsLabelHeightWithTextModel:(TextModel *)textModel;

{

    //size:表示允许文字所在的最大范围

    //options: 一个参数,计算高度时候用 NSStringDrawingUserLineFragmentOrigin

    //attributes:表示文字的某个属性(通常是文字大小)

    //context:上下文对象,通常写nil;

    CGRect rect = [textModel.content boundingRectWithSize:CGSizeMake(290, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]} context:nil];

    

    return rect.size.height;

    

}


// 自定义高度,在tableViewController的.m中得cell高度中调用返回这个方法

+ (CGFloat)cellHeightWithTextModel:(TextModel *)textModel

{

    

    return 90 + [self calsLabelHeightWithTextModel:textModel];

}


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约