1

I have a TabBarController app with a DialogViewController that works fine except that on initial load the table is EMPTY until I touch it or navigate to another tab and back (http://cl.ly/3I0r1v2b420t0L1X1h2w).

I have confirmed the Root is set. I've tried issuing a ReloadData() after. I've also tried setting the TableView.Source directly. In every case the TableView doesn't show anything until another action occurs.

This happens in the simulator and on the iPhone.

Any idea why this might be?

public partial class PapersView : DialogViewController
{
    public PapersView () : base (UITableViewStyle.Plain, null, true)
    {
        EnableSearch = true; 
        AutoHideSearch = true;
        SearchPlaceholder = @"Find Papers";
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        MonoTouch.UIKit.UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
        var svc = new PaperService ();
        svc.GetPapers (onPapersReceived, onErrorReceived);
    }

    private void onErrorReceived (string error)
    {
        MonoTouch.UIKit.UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
    }

    private void onPapersReceived (List<PaperNode> papers)
    {
        MonoTouch.UIKit.UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;

        Root = new RootElement ("Papers") {
                from node in papers
                    group node by (node.paper.title [0].ToString ().ToUpper ()) into alpha
                    orderby alpha.Key
                select new Section (alpha.Key){
                    from eachNode in alpha
                    select (Element)new WhitePaperBible.iOS.UI.CustomElements.PaperElement (eachNode)
        }};

        TableView.ScrollToRow (NSIndexPath.FromRowSection (0, 0), UITableViewScrollPosition.Top, false);
    }
}
Thomas Ruiz
  • 3,611
  • 2
  • 20
  • 33
David Ortinau
  • 271
  • 1
  • 9
  • What events are being called (or not) on the initial load vs the 2nd load? – Jason Apr 10 '12 at 21:40
  • If I understand what you're asking... I set breakpoints in all of the methods and these are only called once when the app initializes. I then touch the UITableView and the data displays as I scroll. Same result if I hit another tab and come back...nothing else is called in that DialogViewController but the data shows up. – David Ortinau Apr 10 '12 at 22:02

2 Answers2

3

Curtis Bailey pointed me on the MonoTouch mailing list to look at InvokeOnMainThread. That solved this issue because the asynchronous service call is on a background thread and so too was the callback.

    private void onPapersReceived (List<PaperNode> papers)
    {
        MonoTouch.UIKit.UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;

        InvokeOnMainThread (delegate {
            Root = new RootElement("Papers") {
                    from node in papers
                        group node by (node.paper.title [0].ToString ().ToUpper ()) into alpha
                        orderby alpha.Key
                    select new Section (alpha.Key){
                        from eachNode in alpha
                        select (Element)new WhitePaperBible.iOS.UI.CustomElements.PaperElement (eachNode)
            }};

            TableView.ScrollToRow (NSIndexPath.FromRowSection (0, 0), UITableViewScrollPosition.Top, false);
        });
    }
David Ortinau
  • 271
  • 1
  • 9
1

Try just populating the existing Root instead of creating a new Root.

If you look in MonoTouch.Dialog sources for DialogViewController, you should find the PrepareRoot() method. Look to see where that is called and it should start to make sense.

jstedfast
  • 35,744
  • 5
  • 97
  • 110