Customising tableviewcell in Xamarin ios with MVVMCross

I am using MVVMCross in my Xamarin application. I am able to display some data in a tableview, Now I need to customise the tableview cell to add accessory and allow user to go to next screen on selection of a row. How can I achieve it? My code to display the tableview:

public partial class SchoolSelectionView : MvxViewController<SchoolSelectionViewModel>
    {
        public SchoolSelectionView() : base("SchoolSelectionView", null)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            this.NavigationController.SetNavigationBarHidden(true,true);

            var source = new MvxStandardTableViewSource(tblSchoolSelection, "TitleText Name");
            this.CreateBinding(source).To<SchoolSelectionViewModel>(vm => vm.Items).Apply();
            this.CreateBinding(source).For(s => s.SelectionChangedCommand).To<SchoolSelectionViewModel>(vm => vm.ItemSelectedCommand).Apply();
            tblSchoolSelection.Source = source;
            //tblSchoolSelection.Source = new SchoolListTableSource(this.ViewModel.Items, this);
            tblSchoolSelection.ReloadData();
        }

        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        }
    }

1 answer

  • answered 2018-01-14 08:45 Cole Xia

    ustomise the tableview cell to add accessory

    Create a subclass inherit from MvxTableViewSource and override the GetOrCreateCellFor method.

    var source = new MyTableViewSource(tblSchoolSelection, "TitleText Name");
    
    public class MyTableViewSource: MvxStandardTableViewSource {
        override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
        {
            var cell= tableView.DequeueReusableCell(CellIdentifier);
            if (cell== null)
            {
                //xxxx
            }
    
            return cell;
        }
    }
    

    Refer to Custom MvxTableViewCell Without a NIB File

    allow user to go to next screen on selection of a row

    Just add ShowViewModel in ItemSelectedCommand in viewModel.

    private IMvxCommand _itemSelectedCommand;
    public IMvxCommand ItemSelectedCommand
    {
        get
        {
            _itemSelectedCommand= _itemSelectedCommand?? new MvxCommand(() => ShowViewModel<NextScreenModel>());
            return _itemSelectedCommand;
        }
    }