繁体   English   中英

MonoTouch.Dialog RootElement的自定义标题

[英]Custom title of MonoTouch.Dialog RootElement

我正在尝试使用Monotouch.Dialog构建菜单结构。 该结构由多个嵌套的RootElements组成。

创建RootElement时,在构造函数中设置标题。 此标题用于表格单元格的文本以及单击它时视图中的标题。

我想将视图的标题设置为与元素名称不同的文本。

让我尝试用简化的例子来说明我的意思。

结构:

- Item 1
  - Item 1.1
  - Item 1.2
- Item 2
  - Item 2.1

创建此结构的代码:

[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
    UIWindow _window;
    UINavigationController _nav;
    DialogViewController _rootVC;
    RootElement _rootElement;

    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
        _window = new UIWindow (UIScreen.MainScreen.Bounds);

        _rootElement = new RootElement("Main");

        _rootVC = new DialogViewController(_rootElement);
        _nav = new UINavigationController(_rootVC);

        Section section = new Section();
        _rootElement.Add (section);

        RootElement item1 = new RootElement("Item 1");
        RootElement item2 = new RootElement("Item 2");

        section.Add(item1);
        section.Add(item2);

        item1.Add   (
                        new Section()
                        {
                            new StringElement("Item 1.1"),
                            new StringElement("Item 1.2")
                        }
                    );

        item2.Add (new Section() {new StringElement("Item 2.1")});

        _window.RootViewController = _nav;
        _window.MakeKeyAndVisible ();

        return true;
    }
}

单击项目1时,将显示标题为“项目1”的屏幕。 我想将标题“Item 1”更改为“Type 1 items”。 但是点击的元素的文本应保持为“第1项”。

处理这个问题的最佳方法是什么?

如果我可以做这样的事情会很好:

RootElement item1 = new RootElement("Item 1", "Type 1 items");

我试过获取DialogViewController(参见这篇文章)并设置它的标题。 但是我没能让它正常工作。

您可以创建RootElement的子类,它会覆盖GetCell的返回值,并在文本成为表视图的一部分时更改要呈现的文本:

 class MyRootElement : RootElement {
      string ShortName;

      public MyRootElement (string caption, string shortName)
          : base (caption)
      {
          ShortName = shortName;
      }

      public override UITableViewCell GetCell (UITableView tv)
      {
           var cell = base.GetCell (tv);
           cell.TextLabel.Text = ShortName;
           return cell;
      }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM