1
votes

I'm new with MVVMCross model for iOS. I want to handle tableview cell tap, and get the tapped cell index. But I don't know how to access the index.

Here is my View code.

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

Here is my ViewModel code.

    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

1 Answers

0
votes

You can try finding the index by passing the selected row object to your Command like this:

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