Issue in Removing UITableViewCell Selection – MvvmCross / Xamarin.iOS

Published

I have a ViewController which contains a TableView and that table view cell contains another TableView. I need to remove cell selection of the child TableView just after tapping on relevant cell, but I can’t find a way to get my work done.

This is my code

Parent TableViewCell Class

public partial class ParentTableViewCell : MvxTableViewCell
{
    protected ParentTableViewCell(IntPtr handle) : base(handle) { }

    public override void AwakeFromNib()
    {
        base.AwakeFromNib();

        MvxTableViewSource childTableViewSource = new ChildTableViewSource(ChildTableView);
        ChildTableView.Source = childTableViewSource;

        var set = this.CreateBindingSet<ParentTableViewCell, ParentCellViewModel>();

        set.Bind(childTableViewSource).To(vm => vm.ChildTableViewItems);
        set.Bind(childTableViewSource).For(tvs => tvs.SelectionChangedCommand).To(vm => vm.ChildTableViewItemsClickCommand);

        set.Apply();
        
        ChildTableView.ReloadData();
    }
}

Child TableView Source Class

public class ChildTableViewSource: MvxTableViewSource
{
    public SubTasksTableViewSource(IntPtr handle) : base(handle) { }

    public SubTasksTableViewSource(UITableView tableView) : base(tableView) { }

    protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
    {
        var cell = (ChildTableViewCell)tableView.DequeueReusableCell("ChildTableViewCell");
        return cell;
    }

    //public override void RowHighlighted(UITableView tableView, NSIndexPath rowIndexPath)
    //{
    //    tableView.DeselectRow(rowIndexPath, true);
    //}

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {
        tableView.DeselectRow(indexPath, true);
    }
}

Parent Cell ViewModel Class

public class ParentCellViewModel : MvxViewModel
{
    protected readonly IMvxNavigationService _navigationService;

    //other parameters

    public BaseViewModel(IMvxNavigationService navigationService)
    {
        _navigationService = navigationService;
    }

    private MvxCommand<ChildCellViewModel> _childTableViewItemsClickCommand;
    public MvxCommand<ChildCellViewModel> ChildTableViewItemsClickCommand => _childTableViewItemsClickCommand =
        _childTableViewItemsClickCommand ?? new MvxCommand<ChildCellViewModel>(OnChildTableViewItemsClickCommand);

    public async void OnChildTableViewItemsClickCommand(ChildCellViewModel item)
    {
        //passing item id to edit view
        MvxBundle bundle = new MvxBundle();
        bundle.Data.Add("ID", item.ID);

        await _navigationService.Navigate<ChildCellEditViewModel, MvxBundle>(bundle);
    }
}

Here what happens is, RowSelected method runs but OnChildTableViewItemsClickCommand doesn’t get hit. As I understand this is because both RowSelected and SelectionChangedCommand are there and I don’t think deselecting cells is possible in ViewModel.

I tried to deselect the cell in RowHighlighted method. But it didn’t deselect cell for some reason.

Can someone show me a way to do both deselecting the row and navigate to edit view at the same time. Any help would be highly appreciated.

Source: Xamarin.ios Questions

Published
Categorised as Uncategorised Tagged , , ,

Answers

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq