Forum Xamarin.iOS
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

UITableViewSource GetHeightForHeader Not Called for height UITableView.AutomaticDimension

SGTHollySGTHolly USMember

The title says it all, but I'll elaborate. I have a Section header that uses autolayout.

In native Swift code, I would write:

class TableViewController: UITableViewController {

    var mySectionHeader: UIView!

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return mySectionHeader
    }

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return UITableViewAutomaticDimension
    }
}

In Xamarin, I write:

    public class TableViewSource : UITableViewSource
    {
        public UIView MySectionHeader;

        public override nfloat GetHeightForHeader(UITableView tableView, nint section)
        {
            return UITableView.AutomaticDimension;
        }

        public override UIView GetViewForHeader(UITableView tableView, nint section)
        {
            return MySectionHeader;
        }
    }

The native code works correctly. However, GetViewForHeader doesn't get called when the value returned by GetHeightForHeader is UITableView.AutomaticDimension. If I change the return value of GetHeightForHeader to any positive integer, it works correctly. I suspect it is because this constant evaluates to -1 and the method should not be called if the height is 0. The native code works. Any suggestions?

Posts

  • TedRogersTedRogers USMember ✭✭✭✭

    You need to set these guys on the UITableView and you don't need to handle GetHeightForHeader().

    tableView.EstimatedSectionHeaderHeight = PropertiesSectionHeaderView.kHeaderHeight;
    tableView.SectionHeaderHeight = UITableView.AutomaticDimension;
    
Sign In or Register to comment.