ios 实现 cell 的动态高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

  Message *msgObj = _items[(NSUInteger) indexPath.row];
  NSString *msg = msgObj.content;
  CGFloat maxWidth = CGRectGetWidth(tableView.bounds);

  CGFloat msgCellWidth = maxWidth - MessageCellAvatarPadding * 2 - AVATAR_HEIGHT - MessageCellPadding-20;
  CGFloat height = [MsgCell heightForMessage:msg constrainedToWidth:msgCellWidth];

  return height+20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  MsgCell *msgItemCell = [tableView dequeueReusableCellWithIdentifier:kMsgCellId forIndexPath:indexPath];
  Message *msg = self.items[(NSUInteger) indexPath.row];
  [msgItemCell setMessage:msg];
  return msgItemCell;
}
原文地址:https://www.cnblogs.com/damnbird/p/4922784.html