2012-08-12 34 views
1

我正在尝试使用Monotouch.Dialog构建菜单结构。该结构由多个嵌套的RootElement组成。MonoTouch.Dialog的自定义标题RootElement

在创建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”。我想将标题“项目1”更改为“类型1项目”。但被点击元素的文本应该仍然是“第1项”。

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

这将是很好,如果我可以做这样的事情:

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

我试图得到DialogViewController(见this后),并设置它的标题。但是我没能使这个工作正常。

回答

4

您可以创建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; 
     } 
} 
+0

谢谢!这就像一个魅力!我确实添加了“返回单元格”;到GetCell。 – Nessinot 2012-08-14 16:36:16