title: Xamarin Forms iOS custom UITableViewCell dynamic height
body: <p>I'm trying to create custom UITableViewCell for Xamarin Forms that will automatically adjust it's height according to the text it contains.</p>
  
  <p>I have found solutions that are made through Designer, but I need to do this in code.</p>
  
  <p>This is what I have now after analyzing the Designer solutions, but it does not work:</p>
  
  <pre><code>public class FormLabelCellView : UITableViewCell, INativeElementView
  {
      public UILabel Label { get; private set; }
      public FormLabelCell Cell { get; set; }
      public Element Element =&gt; Cell;
  
      public FormLabelCellView(FormLabelCell cell) : base(UITableViewCellStyle.Default, cell.GetType().FullName)
      {
          Cell = cell;
          // in the custom ContentView solution I can't set the cell to expand and contract according to the text it contains
          Label = new UILabel();
  
          Label.TranslatesAutoresizingMaskIntoConstraints = false;
          Label.SetContentCompressionResistancePriority(751, UILayoutConstraintAxis.Vertical);
          Label.SetContentHuggingPriority(251, UILayoutConstraintAxis.Vertical);
  
          this.ContentView.AddSubview(Label);
  
          this.ContentView.AddConstraint(NSLayoutConstraint.Create(Label, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this.ContentView, NSLayoutAttribute.Left, 1, 0));
          this.ContentView.AddConstraint(NSLayoutConstraint.Create(Label, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this.ContentView, NSLayoutAttribute.Right, 1, 0));
          this.ContentView.AddConstraint(NSLayoutConstraint.Create(Label, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this.ContentView, NSLayoutAttribute.Top, 1, 0));
          this.ContentView.AddConstraint(NSLayoutConstraint.Create(Label, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this.ContentView, NSLayoutAttribute.Bottom, 1, 0));
      }
  }
  </code></pre>
  
  <p>This is the result (the row height is probably DefaultRowHeight of 44):</p>
  
  <p><a href="https://i.stack.imgur.com/A45K7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/A45K7.png" alt="custom ContentView solution"></a></p>
  
  <p>Before anyone asks about <em>RowHeight</em> and <em>EstimatedRowHeight</em>. Yes I have set <strong>RowHeight = UITableView.AutomaticDimension;</strong> and <strong>EstimatedRowHeight = 24;</strong> in my custom ListViewRenderer.</p>
  
  <p>I can also verify that it works when I modify my custom UITableViewCell to this:</p>
  
  <pre><code>public class FormLabelCellView : UITableViewCell, INativeElementView
  {
      public UILabel Label { get; private set; }
      public FormLabelCell Cell { get; set; }
      public Element Element =&gt; Cell;
  
      public FormLabelCellView(FormLabelCell cell) : base(UITableViewCellStyle.Default, cell.GetType().FullName)
      {
          Cell = cell;
          // this solution works, but I can't remove the margins / padding and set background color
          Label = this.TextLabel;
      }
  }
  </code></pre>
  
  <p>Then the result looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/mLANB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mLANB.png" alt="using TextLabel"></a></p>
  
  <p>I think I'm not creating the constraints correctly. Can someone help me?</p>
  
  <p>Edit: After trying to create custom UITableViewSource for testing purposes I have found that the problem is in the Xamarin implementation of <strong>ListViewDataSource</strong> and <strong>UnevenListViewDataSource</strong>. This is unfortunate because these classes are internal so I can't extend them and override the GetHeightForRow function.</p>