Jeremy Shiklov Jeremy Shiklov - 1 year ago 96
Swift Question

UITableCell Autoresizing with Autolayout doesn't work

I'm programming in Xamarin iOS. Basically the table cell doesn't resize and instead just always stays in a static size (even after typing text and new lines).

I have a xib which contains a UITextView and 4 constrains as needed plus the height constraint(removing the height constraint will make it appear in zero size)

In the ViewDidLoad() method I'm using the following:

TableView.EstimatedRowHeight = 50;
TableView.RowHeight = UITableView.AutomaticDimension;

In ViewWillAppear() I'm also using the same lines.

I also tried using this

public override nfloat EstimatedHeight(UITableView tableView, NSIndexPath indexPath)
return 80;//autodimension also didn't work here.

and finally
GetHeightForRow (UITableView tableView, NSIndexPath indexPath)

None of them worked.

XIB and application

Answer Source

A sample code in Objective-C demonstrates the idea.

The rewritten code in C#

editTextView.Changed += (s, e) => {
                // Sizing: 
                var size = editTextView.SizeThatFits(new CoreGraphics.CGSize(Frame.Width, nfloat.MaxValue));
                var height = size.Height;
                if (height < 50)
                    this.EditTextHeight.Constant = 50;
                    //self.heightConstraint.constant = 50;
                else {
                    this.EditTextHeight.Constant = height;
                    //self.heightConstraint.constant = height;
                // call the tableview-updates in the controller
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download