David Ortinau
David Ortinau

Reputation: 271

MonoTouch DialogViewController is Empty Until Touched

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);
    }
}

Upvotes: 1

Views: 739

Answers (2)

David Ortinau
David Ortinau

Reputation: 271

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);
        });
    }

Upvotes: 3

jstedfast
jstedfast

Reputation: 38528

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.

Upvotes: 1

Related Questions