1

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(この投稿を参照)を取得して、そのタイトルを設定してみました。しかし、これを正しく機能させることができませんでした。

4

1 に答える 1

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;
      }
 }
于 2012-08-12T16:03:05.827 に答える