使用MVVMCross在Xamarin ios中自定义tableviewcell

时间:2018-01-14 08:45:08

标签: uitableview xamarin xamarin.ios mvvmcross

我在我的Xamarin应用程序中使用MVVMCross。我能够在tableview中显示一些数据,现在我需要自定义tableview单元格以添加附件,并允许用户在选择行时转到下一个屏幕。我怎样才能实现它? 我的代码显示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 个答案:

答案 0 :(得分:0)

  

ustomise tableview单元格以添加附件

创建继承自MvxTableViewSource的子类并覆盖GetOrCreateCellFor方法。

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

请参阅Custom MvxTableViewCell Without a NIB File

  

允许用户在选择行时进入下一个屏幕

只需在viewModel中的ShowViewModel中添加ItemSelectedCommand

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