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];
}
}