Bug 58731 - In TableViewRenderer TableView.GetHeaderView returns null
Summary: In TableViewRenderer TableView.GetHeaderView returns null
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: iOS ()
Version: 2.3.5
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-08-11 21:38 UTC by Yuri
Modified: 2018-03-05 15:52 UTC (History)
4 users (show)

Tags: ios tableview
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Yuri 2017-08-11 21:38:36 UTC
In line below 
var hv = tableView.GetHeaderView(s);
"hv" is always 0. It should return Section header.


[assembly: ExportRenderer(typeof(TableView), typeof(CustomTableViewRenderer))]
namespace ButtonRendererDemo.iOS
{
    public class CustomTableViewRenderer : TableViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
                return;

            var tableView = Control as UITableView;
            var numSections = tableView.NumberOfSections();

            for (nint s = 0; s < numSections; s++)
            {
                var hv = tableView.GetHeaderView(s);
                if (hv != null)
                {
                    //do something
                }
            }

        }



    }


}
Comment 1 Yuri 2017-08-11 22:11:34 UTC
What I noticed that on SECOND request of width and height this code starts returning sections. I added 

 protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            var tableView = Control as UITableView;
            var numSections = tableView.NumberOfSections();
            for (nint s = 0; s < numSections; s++)
            {
                var hv = tableView.GetHeaderView(s);
                if (hv != null)
                {
                    hv.TextLabel.Text = "Not Uppercase";
                }
            }

        }

When it is called first time "hv" is still null but as soon as I turn my device I get calls for width and height again and this time it is available.
Comment 2 Jimmy [MSFT] 2017-08-16 18:14:33 UTC
This may be a timing issue because I did see it work once while debugging and running the code you provided inside LayoutSubviews() instead. However, depending on what you are trying to achieve, a better approach is to provide a custom TableViewSource as detailed here: https://alexdunn.org/2017/03/21/xamarin-tips-xamarin-forms-ios-custom-tableview-section-titles/

Note that we also now recommend using a ListView if possible instead of a TableView. A ListView supports grouping[1] and more customization options without needing a custom renderer[2]. Hope this helps! 

[1] http://motzcod.es/post/94643411707/enhancing-xamarinforms-listview-with-grouping
[2] https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/customizing-list-appearance/
Comment 3 Yuri 2017-08-16 18:44:09 UTC
I have not tried custom renderer of ListView, so I cannot comment on that and how flexible it is.

For TableView it might be working in LayoutSubviews() and it definitely is working in Draw() function (that was workaround for me). My point is that it should be working in OnElementChanged as UITableView is already constructed.
Comment 4 Paul DiPietro [MSFT] 2018-03-05 15:52:54 UTC
Please open a new issue on GitHub with a reproduction project if this still occurs as of the latest versions of Forms/Xamarin.iOS as we're using that to track issues now. Thanks.