MVVMCross iOS表视图单元格绑定

时间:2017-10-30 03:02:40

标签: xamarin xamarin.ios mvvmcross

我是iOS的MVVMCross模型的新手。我想处理tableview单元格,并获取tapped cell索引。但我不知道如何访问索引。

这是我的查看代码。

        var menuSource = new MenuTableViewSource(menuTableView, MenuCell.Key, MenuCell.Key);
        this.menuTableView.Source = menuSource;

        var set = this.CreateBindingSet<BooksView, BooksViewModel>();
        set.Bind(menuSource).To(vm => vm.MenuCellTexts);
        set.Bind(menuSource).For(s => s.SelectionChangedCommand).To(vm => vm.ItemSelectedCommand);
        set.Apply();

这是我的ViewModel代码。

    private MvxCommand _itemSelectedCommand;
    public MvxCommand ItemSelectedCommand
    {
        get
        {
            _itemSelectedCommand = _itemSelectedCommand ?? new MvxCommand(DoSelectedItem);
            return _itemSelectedCommand;
        }
    }

    private void DoSelectedItem()
    {
        // How to get the tapped cell index here??
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试通过将所选行对象传递到Command来查找索引:

private MvxCommand<YourClassName> _itemSelectedCommand;
public MvxCommand<YourClassName> ItemSelectedCommand
{
    get
    {
        _itemSelectedCommand = _itemSelectedCommand ?? new MvxCommand(DoSelectedItem);
        return _itemSelectedCommand;
    }
} 

private void DoSelectedItem(YourClassName item)
{
    // How to get the tapped cell index here??

    var index = MenuCellTexts.IndexOf(item);
}