2017-01-09 60 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)); 
    } 
} 

附加信息:

FlavourSetItemViewModel包含FlavourSetItemViewModel列表。我會嘗試以下解釋:

  • FlavourSetItemViewModel - 有節的名稱
    • FlavourSetItemViewModel - 有項目名稱
    • FlavourSetItemViewModel - 有項目名稱
    • FlavourSetItemViewModel - 有項目名稱
    • FlavourSetItemViewModel - 商品名稱
  • 口味etItemViewModel - 有節的名稱
    • FlavourSetItemViewModel - 有項目名稱

回答

1

你在做什麼不看我錯了。你可以改進的是使用CompositeDisposable而不是IDisposable列表。

但是你也可以嘗試使用ReactiveTableViewSource通過使用自己的實現,然後覆蓋GetViewForHeader來提供自己的標題視圖。

然後您可以將數據綁定到您的自定義表視圖來源:

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); 

如果您使用的部分的動態量,你可以做到以下幾點:

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是一個二維的排序列表。

請注意,在我的示例中,未對列表中的更改進行考慮。只改變屬性(TableSections)本身。

+0

這看起來很有前途。但我有一個動態的部分。任何提示? – angak

+0

我已將此添加到答案中。 – Qonstrukt

+0

顯然不是所有的腦細胞今天都在工作。請您詳細說明「TableSectionInformation」的實現嗎? – angak

相關問題