全文展開收縮

需求:消息如果少于三行,正常顯示,如果超過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,533評論 2 7
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,229評論 4 61
  • 概述在iOS開發中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,088評論 3 38
  • 2017.02.22 可以練習,每當這個時候,腦袋就犯困,我這腦袋真是神奇呀,一說讓你做事情,你就犯困,你可不要太...
    Carden閱讀 1,373評論 0 1
  • 上周末,我和老公大吵了一架。 起因是,大周末的,我在打掃衛生,他在打游戲,我叫他幫忙,他說馬上就來,再叫還是馬上就...
    熱帶魚bh閱讀 93評論 0 1