2012-08-12 71 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