tableView的cell高度自适应

1,给定内容,宽度,文本的一些属性计算高度

+ (CGFloat)getHeight:(NSString *)content{
    CGRect rect = [content boundingRectWithSize:CGSizeMake(kWidth - 24 - 30 - K_margin, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    return rect.size.height;
}

可以用上面的方法,加上一些其他除计算的label的之外控件的高度,算出整个cell的高度
用这个方法在layoutSubViews里不用设置lable的高度
将每一个row的高度算出后存在数组里,缓存下来,在协议里直接返回数组

- (void)calculateOnHeight:(ZJICommentsModel *)commentsModel{
    for(int i = 0;i < commentsModel.comments.count;i++){
        CommitModel * model = commentsModel.comments[i];
        ReplytoModel *replyModel = [commentsModel.comments[i] replyto];
        NSString *contentString = nil;
        CGFloat contentHeight = 0.0;
        if(replyModel){
            contentString = [NSString stringWithFormat:@"%@\n\n//%@:%@",[model content],[replyModel author], [replyModel content]];
        }else{
            contentString = [NSString stringWithFormat:@"%@",[model content]];
        }
        contentHeight = [ZJILongCommitTableViewCell getCellHeight:contentString];
        NSNumber *height = [NSNumber numberWithFloat:contentHeight];
        if([commentsModel isEqual:self.customView.longCommentModel]){
            [_cellHeightOnArray  addObject:height];
        }
        else{
            [_cellShortHeightOnArray addObject:height];
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 ) {
        ReplytoModel *replyModel = [self.customView.longCommentModel.comments[indexPath.row] replyto];
        if([self.extraModel long_comments] == 0){
            return 0.0;
        }else{
            if(replyModel && replyModel.isOpening){
                return [_cellHeightOnArray[indexPath.row] floatValue];
            }
            return [_cellHeightArray[indexPath.row] floatValue];
        }
    }else{
        ReplytoModel *replyModel =  [self.customView.shortCommentModel.comments[indexPath.row] replyto];
        if(replyModel.isShortOpening){
            return [_cellShortHeightOnArray[indexPath.row] floatValue];
        }
        return [_cellShortHeightArray[indexPath.row] floatValue];
    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值