Auto layout not working till row/table is reloaded RRS feed

  • Question

  • User243147 posted

    I have a custom table cell which i'm using auto layout to determine hight of cell.

    in my view controller ViewDidLoad() I have

    tableView.EstimatedRowHeight = 150; tableView.RowHeight = UITableView.AutomaticDimension;

    I set up all the constraints on storyboard and it appears to have all necessary constraints to make auto layout work.

    But when I load the page I get

    I have some logic that reloads the row if user taps on the check box on the left. After that is tapped the row adjusts properly to

    (Will also fix itself if user scrolls down then back up, pretty much any reload will fix it)

    Notice that after cell is reloaded the address properly wraps and expands. But on initial page load the last word of the address is cut off because the label is not expanding vertically.
    Everything except that address label has a height constraint and all bottom labels have a constraint to the bottom of the view. If I remove the height constraint from the top most bolded label the address expands properly but the top label shrinks. I tried messing with CompressionResistance but no matter what I have either the top most label shrinking when it shouldn't, or have the address label cutting of second line of text.

    I am able to fix the issue by

                    tableView.BeginUpdates ();
                    tableView.ReloadRows (_mainTableView.IndexPathsForVisibleRows, UITableViewRowAnimation.Automatic);
                    tableView.EndUpdates ();

    but this creates some strange behavior when the page loads because things are moving around.

    Code snippet from ViewDidLoad()

                    _mainTableView.EstimatedRowHeight = 140;
                    _mainTableView.RowHeight = UITableView.AutomaticDimension;
                    _mainTableView.Hidden = true;
                    _activityIndicator.Hidden = false;
                    _mainTableView.Source = new CustomTableViewSource();
                    _mainTableView.ReloadData ();
                    // shouldn't be nescessary but auto layout does not seem to work on first reload
                    //_mainTableView.BeginUpdates ();
                    //_mainTableView.ReloadRows (_mainTableView.IndexPathsForVisibleRows, UITableViewRowAnimation.Automatic);
                    //_mainTableView.EndUpdates ();
                    _mainTableView.Hidden = false;
                    _activityIndicator.Hidden = true;

    My first instinct is to mess with constraints and priorities but the fact that it works perfectly after a second reload is a bit strange.

    Thursday, August 25, 2016 5:01 PM


  • User243147 posted

    Solved by adding await Task.Yield() after reload but before it is shown. Apparently main thread needs control to update auto layout before dismissing activity indicator and showing table.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, August 30, 2016 2:30 PM