2017-09-24 4 views
0

私は頻繁にtableSectionsを削除して別のものに置き換えるアプリケーションを持っています。 (tableView.Root場合TableSectionの名前を指定してTableSectionを削除して追加することはできますか?

"カード" と呼ばれる - "グループ"

  • TableSection 2と呼ばれる - - TableSection 1

  • "ヘッダー" と呼ばれる

    • TableSection 0:ここで私が使用して典型的なコードです.Count> 2) tableView.Root.RemoveAt(2); // "Cards"という名前のセクションを削除する tableView.Root.Add(CreateTableSection());

    コードを少しきれいにしたいと思います。セクションに割り当てた名前と名前付きセクションの後に追加できる方法を使用してtableSectionを削除できる方法はありますか?

  • 答えて

    2
    var header = tableView.Root.FirstOrDefault(r => r.Title == "Header"); 
    
    if (header != null) { 
        tableView.Root.Remove(header); 
    } 
    

    また、作成時にヘッダーセクションへの参照を保持し、後でその参照を使用して後で削除することもできます。これにより、削除する前にそれを見つける手間を省くことができます。

    +0

    感謝。参照の維持に関する問題は、ヘッダーがXAMLで最初に作成されるため、私はそれが不可能であると推測しているということです。しかし、それを見つける方法に関するあなたの提案は完璧です。ありがとう – Alan2

    0

    TabelRootの拡張メソッドを作成し、よりクリーンなコードを作成できます。

    拡張メソッド

    public static class Extensions 
    { 
        public static TableSection FindSection(this TableRoot root, string title) 
        { 
         return root.FirstOrDefault(r => r.Title == title); 
        } 
    
        public static bool AppendAfter(this TableRoot root, string title, TableSection toBeAdded) 
        { 
         var section = root.FindSection(title); 
         if (section != null) 
         { 
          var index = root.IndexOf(section); 
          root.Insert(index + 1, toBeAdded); 
         } 
         return false; 
        } 
    
        public static bool AppendBefore(this TableRoot root, string title, TableSection toBeAdded) 
        { 
         var section = root.FindSection(title); 
         if (section != null) 
         { 
          var index = root.IndexOf(section); 
          root.Insert(index, toBeAdded); 
         } 
         return false; 
        } 
    
        public static bool Remove(this TableRoot root, string title) 
        { 
         var section = root.FindSection(title); 
         if (section != null) 
          return root.Remove(section); 
         return false; 
        } 
    
        public static bool Replace(this TableRoot root, TableSection newSection) 
        { 
         var section = root.FindSection(newSection?.Title); 
         if (section != null) 
         { 
          var index = root.IndexOf(section); 
          root.RemoveAt(index); 
          root.Insert(index, newSection); 
         } 
         return false; 
        } 
    } 
    

    使用

    var root = tableView.Root; 
    root.Remove("Section 2"); 
    root.AppendAfter("Section 1", toBeAdded: section4 }); 
    root.AppendBefore("Section 2", toBeAdded: section0 }); 
    root.AppendBefore("Section 4", toBeAdded: new TableSection { Title = "Section 3" }); 
    
    関連する問題