3

For an application that dynamically changes which inputs are shown on the screen I create all possible Elements and then create a Section which contains the ones I need given the current data.

If the user interacts with a BooleanElement for example, the form would need to be updated and fields shown accordingly.

Apparently this pattern doesn't work, as shown by this test case:

        var dateEntry = new DateTimeElement("Timestamp", DateTime.Now);

        var section = new Section() { dateEntry };
        var root = new RootElement("Root") { section };
        var dvc = new DialogViewController(root);

        window.RootViewController = dvc;

        NSTimer.CreateScheduledTimer (TimeSpan.FromSeconds (2), delegate {
            var newSection = new Section() { dateEntry };
            dvc.Root.Clear();
            dvc.Root.Add(newSection);
        });

The above code throws a NullReferenceException in DateTimeElement.FormatDate once the timer is up.

I also tried recreating not only the Section but also the RootElement, but to no avail.

Is there any recommended pattern to reuse the Elements, or should I just create new inputs whenever the data changes?

Timm
  • 2,652
  • 2
  • 25
  • 34

1 Answers1

3

this should do the trick for you, and you can remove the NStimer

        this.Root.Remove(section);
        this.Root.Insert(0,UITableViewRowAnimation.Fade,newSection);
Janub
  • 1,594
  • 14
  • 26
  • The NSTimer is just there to simulate real world usage, in this case the user clicking a button a few seconds after the screen appears. I will try your solution and see if it works. – Timm Mar 06 '12 at 16:22
  • Your approach just leads to a frozen UI for me. I think I will go and recreate a new `Section` and add that to the root. – Timm Mar 10 '12 at 13:23
  • Ok, I tried your code with the example I posted above and it worked, strangely it didn't with my production code, which is mostly similar. Will accept, since you fixed the issue I presented, and maybe work out another testcase. Thanks for your help! – Timm Mar 10 '12 at 13:46
  • Same issue for me. Using the accepted answer (or re adding the sections with Root.Add(section)) produce a frozen UI with the section and its sub-elements (elements are visible but not usable). For example, impossible to select a sub RootElement with a group and RadioElements inside. The same structure (section and sub elements) works well if not added/removed dynamically at runtime from the dvc. I've tried to override the RootElement to detect if the row is Selected, but it is not the case. Only the sections added/removed seem to be impacted (I have other sections in the same dvc which works f –  Nov 01 '13 at 00:23