Как вы настроили свою ViewModel?Вы должны привязать ваш TableView.Source
ItemsSource к вашей ViewModel, а затем установить свои данные там:
Вот мой код привязки в View :
var source = new TableSource(TableView);
TableView.Source = source;
TableView.RowHeight = 120f;
TableView.ReloadData();
var set = this.CreateBindingSet<FirstView, FirstViewModel>();
set.Bind(source).For(s => s.ItemsSource).To(vm => vm.ItemsGroup);
set.Apply();
ViewModel может выглядеть следующим образом :
public class FirstViewModel : MvxViewModel
{
public FirstViewModel()
{
// Emulate three groups here.
ItemsGroup = new List<SessionGroup>();
for (int i=0; i<3; i++)
{
var list = new List<Item>();
for (int j=0; j<10; j++)
{
list.Add(new Item { Name = "Section:" + i + "Item" + j });
}
ItemsGroup.Add(new SessionGroup("section" + i, list));
}
}
private List<SessionGroup> _ItemsGroup;
public List<SessionGroup> ItemsGroup
{
get
{
return _ItemsGroup;
}
set
{
_ItemsGroup = value;
RaisePropertyChanged(() => ItemsGroup);
}
}
}
public class Item
{
public string Name { set; get; }
}
public class SessionGroup : List<Item>
{
public string Key { get; set; }
public SessionGroup(string key, List<Item> items) : base(items)
{
Key = key;
}
}
После этой привязки мы можем представить TableView.вот мой MvxTableViewSource
:
public class TableSource : MvxTableViewSource
{
private static readonly NSString CellIdentifier = new NSString("MyTableViewCell");
public TableSource(UITableView tableView)
: base(tableView)
{
tableView.SeparatorStyle = UITableViewCellSeparatorStyle.None;
tableView.RegisterNibForCellReuse(UINib.FromName("MyTableViewCell", NSBundle.MainBundle),
CellIdentifier);
}
protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
{
return TableView.DequeueReusableCell(CellIdentifier, indexPath);
}
protected override object GetItemAt(NSIndexPath indexPath)
{
var _sessionGroup = ItemsSource.ElementAt(indexPath.Section) as SessionGroup;
if (_sessionGroup == null)
return null;
return _sessionGroup[indexPath.Row];
}
public override nint NumberOfSections(UITableView tableView)
{
return ItemsSource.Count();
}
public override nint RowsInSection(UITableView tableview, nint section)
{
var group = ItemsSource.ElementAt((int)section) as SessionGroup;
return group.Count();
}
public override string TitleForHeader(UITableView tableView, nint section)
{
var group = ItemsSource.ElementAt((int)section) as SessionGroup;
return string.Format($"Header for section {group.Key}");
}
}
Обновление:
Если вы хотите связать свой ItemsSource
с Dictionary<>
, просто изменитеGetItemAt()
событие:
protected override object GetItemAt(NSIndexPath indexPath)
{
return savedItemList.Values.ToList()[indexPath.Section][indexPath.Row];
}
public override nint NumberOfSections(UITableView tableView)
{
if (ItemsSource != null)
{
savedItemList = (Dictionary<string, List<Item>>)ItemsSource;
return savedItemList.Count();
}
return 0;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
if (ItemsSource != null)
{
savedItemList = (Dictionary<string, List<Item>>)ItemsSource;
return savedItemList.Values.ToList()[(int)section].Count;
}
return 0;
}
В нем указывается Модель, которая будет использоваться в классе ячеек.
Я тоже обновляю демо 1033 *, пожалуйста, проверьте его.