2017-01-09 5 views
0

ReactiveUIを使用してアプリでいくつかの作業を行ってきました。UITableViewCellsでのバインディングの処理方法

私の問題は、UITableViewsとセルの再利用です。私はReactiveTableViewSourceを使ってみましたが、それは私が望むカスタマイズレベル(カスタムヘッダーとフッタービュー)を与えてくれていないようです。

私はUITableViewSourceとUITableViewCellを使用し、IViewForを実装しました。 それから、私は個々の細胞で結合を行った。

これは問題なく動作しますが、セルが再利用されるたびに新しいViewModelに「再バインド」することが心配です。私はそれを取り組んだと信じていますが、もっと良い方法があると確信しています。私が使用している


コード(のみ関連ビット):

public partial class FlavourTableViewCell : UITableViewCell, IViewFor<MenuItemViewModel.FlavourSetItemViewModel> 
{ 
    public MenuItemViewModel.FlavourSetItemViewModel ViewModel { get; set; } 
    object IViewFor.ViewModel { get { return ViewModel; } set { ViewModel = (MenuItemViewModel.FlavourSetItemViewModel)value; } } 

    List<IDisposable> bindings = new List<IDisposable>(); 

    // Called when new data should be shown in cell 
    internal void Update (MenuItemViewModel.FlavourSetItemViewModel data, MenuItemViewModel.FlavourSelectionEnum type) 
    { 
     ViewModel = data; 

     // Clear old bindings 
     if (bindings.Any()) { 
      bindings.ForEach (x => x.Dispose()); 
      bindings.Clear(); 
     } 

     bindings.Add (this.Bind (ViewModel, x => x.IsSelected, view => view.SelectionButton.Selected)); 
    } 
} 

追加情報:

FlavourSetItemViewModelFlavourSetItemViewModelのリストが含まれています。私は以下に説明しようとするでしょう:

  • FlavourSetItemViewModelを -
    • FlavourSetItemViewModelをセクション名を持っています -
    • FlavourSetItemViewModelをアイテム名を持っています -
    • FlavourSetItemViewModelをアイテム名を持っています -
    • FlavourSetItemViewModelをアイテム名を持っています - 商品名を持っています
  • フレーバーetItemViewModelは -
    • FlavourSetItemViewModelをセクション名を持っています - あなたがやっていることは私には間違って見ていない

答えて

1

項目名を持っています。改善できる点は、IDisposableのリストの代わりにCompositeDisposableを使用しています。

ReactiveTableViewSource独自の実装を使用し、GetViewForHeaderを上書きして独自のヘッダービューを提供することもできます。

次に、あなたのカスタムテーブルビューのソースにデータをバインドすることができます。

this.WhenAnyValue (view => view.ViewModel.TableSections) 
    .Select (tableData => tableData.Select (section => new TableSectionInformation<TModel, TReactiveTableViewCell> (section, "cellIdentifier", 44)).ToArray()) 
    .BindTo (tableViewSource, x => x.Data); 

TableDataと仮定すると:

this.WhenAnyValue (view => view.ViewModel.Section1, view => view.ViewModel.Section2, (section1, section2) => new TableSectionInformation<TModel> [] { 
    new TableSectionInformation<TModel, TReactiveTableViewCell> (section1, "section1cell", 44), 
    new TableSectionInformation<TModel, TReactiveTableViewCell> (section2, "section2cell", 44) 
}) 
.BindTo (tableViewSource, x => x.Data); 

あなたはセクションの動的な量を使用している場合は、あなたが次のことを行うことができますソートの2次元リストです。

私のサンプルでは、​​リストの変更は考慮されていないことに注意してください。プロパティ(TableSections)自体の変更のみ。

+0

これは有望です。しかし、私は動的な数のセクションを持っています。それについてのヒント? – angak

+0

私はこれを答えに加えました。 – Qonstrukt

+0

今日私の脳細胞のすべてが明らかに働いているわけではありません。 'TableSectionInformation'の実装について詳しく教えてください。 – angak

関連する問題