0

我有一个RadioGroup非常多RadioElements的子DialogViewController

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

我想启用搜索此嵌套 DVC 以使选择正确的RadioElement更简单。因此,我实现了一个自定义RootElement,它结合了传递一个组和创建一个 DVCEnableSearch并使用它而不是上面的那个:

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

不幸的是,在输入子 DVC 的搜索栏时,我遇到了以下崩溃:

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 

为什么会崩溃以及如何归档我上面描述的功能?

4

2 回答 2

0

此处的错误报告包括针对您遇到的问题的根本原因的解决方法,但还讨论了过滤将如何导致即使在应用过滤器后将第 n 个元素标记为选中的可用性问题。

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

如果您不想更新核心 MTD 代码,您可以通过将其放入您自己的 UIBarSearchDelegate 来使用相同的技术。不幸的是,默认的 SearchDelegate 类是内部的,因此您需要在委托中添加所有代码。我能够做到这一点并让它在不更改 MTD 源的情况下工作:

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

然后你使用它而不是基本方法:

public override void TextChanged (UISearchBar searchBar, string searchText)
{
    container.PerformFilter (searchText ?? "");
    foreach (var s in container.Root)
        s.Parent = container.Root;
}
于 2014-09-28T15:58:50.827 回答
0

我不能给你一个直接的答案,但你可能(假设你的来源与这个不同步)想看看第 1066 行 -

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

if (!(root.group is RadioGroup))

根为空吗?考虑下载 MTD 源代码并对其进行调试,检查您是如何创建 DVC 的。

您还可以用几个硬编码部分替换您的 LINQ,确保这不是您的问题。

希望这可以帮助

于 2013-06-24T14:06:10.063 回答