需求:消息如果少于三行,正常顯示,如果超過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