美文网首页
全文展开收缩

全文展开收缩

作者: 秋S寂S | 来源:发表于2017-11-08 11:18 被阅读32次
需求:消息如果少于三行,正常显示,如果超过3行,只显示三行,尾部加上展开按钮,点击按钮展开,再次点击收缩

思路

  • 通过model来存储是否显示按钮,显示高度
  • 按钮回调里修改model中的属性值,刷新某一个cell
  • 需要注意的是当前tableView的estimatedRowHeight需要设置0,不然回刷新动画会出现乱跳的情况
计算model
// 计算相关高度
for (MessageMo *msgMo in tmpData) {
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    [attributes setObject:FONT_FT2 forKey:NSFontAttributeName];
    // lable的宽度
    CGFloat width = SCREEN_WIDTH - 30 - 15 - 70 - 30;
    CGFloat textH = [msgMo.messageContent boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attributes context:nil].size.height;
                
    CGFloat lineHeight = FONT_FT2.lineHeight;
    NSInteger lineCount = textH / lineHeight;
                
    if (lineCount <= 3) {
        msgMo.showBtn = NO;
        msgMo.btnShowSelect = NO;
        msgMo.cellHeightMax = 119;
        msgMo.cellHeightMin = 119;
    } else {
        msgMo.showBtn = YES;
        msgMo.btnShowSelect = NO;
        msgMo.cellHeightMin = 135;
        msgMo.cellHeightMax = textH + 43 + 10 + 30;
    }
}

// cell赋值
if (_model.showBtn) {
    [self.btnMore mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView);
        make.right.equalTo(self.labTime);
        make.width.equalTo(42);
        make.height.equalTo(30);
    }];
        
    self.btnMore.selected = _model.btnShowSelect;       
    if (_model.btnShowSelect) {
        self.labMsg.numberOfLines = 0;
    } else {
        self.labMsg.numberOfLines = 3;
    }
}
[图片上传中...(视频.gif-dda4dd-1510111043958-0)]


// 回调方法
- (void)messageCellDidSelected:(CourseMessageCell *)cell {
    NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
    MessageMo *msgMo = self.data[indexPath.row];
    
    if (msgMo.showBtn) {
        msgMo.btnShowSelect = !msgMo.btnShowSelect;
        [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

视频.gif

相关文章

网友评论

      本文标题:全文展开收缩

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