全文展开收缩

需求:消息如果少于三行,正常显示,如果超过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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我们在上一篇《通过代码自定义不等高cell》中学习了tableView的相关知识,本文将在上文的基础上,利用sto...
    啊世ka阅读 1,543评论 2 7
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,093评论 3 38
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,383评论 0 1
  • 上周末,我和老公大吵了一架。 起因是,大周末的,我在打扫卫生,他在打游戏,我叫他帮忙,他说马上就来,再叫还是马上就...
    热带鱼bh阅读 94评论 0 1