0

I have a RadioGroup with very many RadioElements as a Sub-DialogViewController:

Root.Add(
    new Section() {
        new RootElement ("Demo", new RadioGroup ("demogroup", 0)) {
            new Section () {
                from demoItem in bigItemList
                    select (Element) new RadioElement (demoItem)
            }
        }
    }
);

I want to enable Search for this nested DVC to make picking the right RadioElement simpler. Therefor I implemented a custom RootElement which combines passing a Group and creating a DVC with EnableSearch and used it instead of the one above:

using System.Collections.Generic;

namespace MonoTouch.Dialog
{
    public class SearchableRootElement : RootElement
    {
        public SearchableRootElement(string caption, Group group) : base(caption, group)
        {
            this.createOnSelected = x => {
                return new DialogViewController(x) { EnableSearch = true }; 
            };
        }
    }
}

Unfortunately when typing into the Searchbar of the sub DVC I get the following crash:

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at MonoTouch.Dialog.RadioElement.GetCell (MonoTouch.UIKit.UITableView tv) [0x00019] in /Developer/MonoTouch/Source/MonoTouch.Dialog/MonoTouch.Dialog/Elements.cs:1066 
  at MonoTouch.Dialog.DialogViewController+Source.GetCell (MonoTouch.UIKit.UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) [0x00029] in /Developer/MonoTouch/Source/MonoTouch.Dialog/MonoTouch.Dialog/DialogViewController.cs:341 
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 
  at Demo.iOS.Application.Main (System.String[] args) [0x00001] in /Users/rodjatrappe/Projects/Claas/Dev/Apps/Demo.iOS/Main.cs:16 
2013-06-22 14:15:02.296 DemoiOS[547:21b03] Unhandled managed exception: Object reference not set to an instance of an object (System.NullReferenceException)
  at MonoTouch.Dialog.RadioElement.GetCell (MonoTouch.UIKit.UITableView tv) [0x00019] in /Developer/MonoTouch/Source/MonoTouch.Dialog/MonoTouch.Dialog/Elements.cs:1066 
  at MonoTouch.Dialog.DialogViewController+Source.GetCell (MonoTouch.UIKit.UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) [0x00029] in /Developer/MonoTouch/Source/MonoTouch.Dialog/MonoTouch.Dialog/DialogViewController.cs:341 
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 
  at Demo.iOS.Application.Main (System.String[] args) [0x00001] in /Users/rodjatrappe/Projects/Claas/Dev/Apps/Demo.iOS/Main.cs:16 

Why is it crashing and how to archive the feature I described above?

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
Rodja
  • 7,998
  • 8
  • 48
  • 55

2 Answers2

0

I cannot give you a direct answer but you may (assuming your source is not out of sync with this one) want to have a look at Line 1066 -

https://github.com/migueldeicaza/MonoTouch.Dialog/blob/master/MonoTouch.Dialog/Elements.cs

if (!(root.group is RadioGroup))

Is root null? Consider downloading the MTD source code and debugging it, check how you are creating your DVC.

You could also replace your LINQ with a couple of hard coded sections, ensure that is not your issue.

Hope this helps

WickedW
  • 2,331
  • 4
  • 24
  • 54
0

The bug report here includes a workaround for the root cause of the issue you're experiencing, but also talks about how filtering will then cause a usability issue of marking the nth element as selected even after a filter has been applied.

https://github.com/migueldeicaza/MonoTouch.Dialog/issues/203

If you don't want to update the core MTD code, you could use that same technique by putting it in your own UIBarSearchDelegate. Unfortunately, the default SearchDelegate class is internal, so you'll need to add all of the code in your delegate. I was able to do this and get it working without changing the MTD source:

    public override void LoadView()
    {
        base.LoadView();
        ((UISearchBar)TableView.TableHeaderView).Delegate = new MySearchBarDelegate(this);
    }

And then you use this instead of the base method:

public override void TextChanged (UISearchBar searchBar, string searchText)
{
    container.PerformFilter (searchText ?? "");
    foreach (var s in container.Root)
        s.Parent = container.Root;
}
danmiser
  • 1,083
  • 12
  • 17
  • Thanks for this. Ran into the same issue and party solved it using your suggestion. However, how does this work around the usability issue of marking the nth element as selected even after a filter has been applied? Does this issue still persist in your suggested solution or am I missing part of the point? thanks! – Corstiaan Mar 15 '15 at 15:19