Adjust UITableViewCell height according to the resizing label
<p>I have a custom <code>UITableViewCell</code>. In UITableViewCell's <code>contentView</code> i have added a <code>UIButton</code> and below button there is a <code>UILabel</code> (Button and UILabel are siblings to each other). </p>

<p>In the start there will no text on the UILabel and so its height constraint is set to >= 0. but When i tap the button, i set the text property of the <code>UILabel</code>, and it should become visible. In other words i want to expand the height of the UILabel and also height of UITableViewCell should be expanded.</p>

<p>I have pinned <code>UIButton</code> <code>Top,Leading</code> and <code>Trailing</code> edges to <code>UITableViewCell</code> <code>contentView</code> and similarly i have pinned UILabel's <code>Bottom,Leading</code> and <code>Trailing</code> edges to <code>UITableViewCell</code> contentsView.</p>

<p>Now problem is that when i tap <code>UITableViewCell</code> it does not resize <code>UILabel</code> and also does not adjust the <code>UITableViewCell</code>.</p>

<p>Here is my code for the </p>

<pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


  MYCustomCell *cell = (MYCustomCell *) [tableView cellForRowAtIndexPath:indexPath];
  cell.descriptionLbl.text = @"This is my text";
  [cell layoutSubviews];
}
</code></pre>

<p>Similarly</p>

<pre><code>- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

  MYCustomCell *cell = (MYCustomCell *) [tableView cellForRowAtIndexPath:indexPath];
  cell.descriptionLbl.text = @"";
  [cell layoutSubviews];
}
</code></pre>

<p>Here is the layoutSubViews of my Custom Cell</p>

<pre><code>- (void) layoutSubviews {
  [super layoutSubviews];

  self.descriptionLbl.preferredMaxLayoutWidth = CGRectGetWidth(self.contentView.bounds);
  self.descriptionLbl.numberOfLines = 0;
  [self.descriptionLbl sizeToFit];
  if (self.descriptionLbl.text.length &gt; 0){
    self.descriptionLbl.frame  = CGRectMake(self.descriptionLbl.frame.origin.x, self.descriptionLbl.frame.origin.y, self.descriptionLbl.frame.size.width, 50); // Here the size will be calculated more accurately later
  }
}
</code></pre>

<p>Just a note that i am not setting the height of the <code>UITableViewCell</code> anywhere.</p>