C# 从未调用TableViewsource的Mvvmcross-GetOrCreateCellFor
我正在尝试设置viewmodel和tableviewsource之间的绑定。但从未调用tableviewsource中的GetOrCreateCellFor方法 这是我的密码: 视图控制器:C# 从未调用TableViewsource的Mvvmcross-GetOrCreateCellFor,c#,xamarin.ios,mvvmcross,C#,Xamarin.ios,Mvvmcross,我正在尝试设置viewmodel和tableviewsource之间的绑定。但从未调用tableviewsource中的GetOrCreateCellFor方法 这是我的密码: 视图控制器: public partial class MainView : MvxViewController { public MainView() : base("MainView", null) { } public override void ViewDidLoad()
public partial class MainView : MvxViewController
{
public MainView() : base("MainView", null)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
var source = new TableViewDataSource(FloorTableView);
this.CreateBinding(source).To((MainViewModel vm) => vm.Floors).Apply();
FloorTableView.Source = source;
FloorTableView.ReloadData();
}
}
视图模型:
public class MainViewModel : MvxViewModel
{
DataService DataService;
ObservableCollection<Category> _Floors = new ObservableCollection<Category>();
public ObservableCollection<Category> Floors
{
get
{
LoadFloors();
return _Floors;
}
set
{
_Floors = value; RaisePropertyChanged(() => Floors);
}
}
void LoadFloors()
{
_Floors.Add(new Category {
Name = "Test"
});
}
}
FloorCell是简单的空tableview单元格,它扩展了MvxTableViewCell
问题出在哪里?您需要实现rowsinssection
方法,否则将无法调用该方法
您还需要确保
UITableView
实际上有一个Frame
设置,可以是您自己设置的,也可以是通过自动布局约束设置的。如果高度或宽度为0,则也不会调用该方法。如果表视图的高度至少低于项目(包括页脚/页眉大小),也可能发生这种情况。因此,请确保适当地计算高度(有一项可用,它将向表视图添加滚动,但会调用GetOrCreateCell)
public class TableViewDataSource : MvxTableViewSource
{
private static string CellId = "FloorCell";
public TableViewDataSource(UITableView tableView) : base(tableView)
{
tableView.RegisterNibForCellReuse(FloorCell.Nib, CellId);
}
public override System.nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
{
return 50;
}
protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
{
//this method never called
return tableView.DequeueReusableCell(CellId, indexPath);
}
}