Question

Hi I'm having an issue trying to bind MvxTableViewCell accessory Checkmark to a local property. I've tried following an example found at Bind MvxBindableTableViewCell's Accessory to boolean

I'm quite new to IOS and even newer to MvvmCross so I apologize if I've made any silly mistakes

public partial class TaxaListCellView : MvxTableViewCell
{
    public static readonly UINib Nib = UINib.FromName ("TaxaListCellView", NSBundle.MainBundle);
    public static readonly NSString Key = new NSString ("TaxaListCellView");


    public TaxaListCellView (IntPtr handle) : base (handle)
    {
        Accessory = UITableViewCellAccessory.Checkmark;
        SelectionStyle = UITableViewCellSelectionStyle.None;

        this.DelayBind (() => {
            var set = this.CreateBindingSet<TaxaListCellView, TaxonViewModel>();
            set.Bind(lblSelectedTaxon).To(s => s.Name);
//I've been playing around with both ways below, and a few different 
//variants without any success
            //set.Bind("IsChecked").To(s => s.IsSelected).TwoWay();
            //set.Bind(@"'IsChecked':{'Path':'IsSelected'");
            set.Apply();
        });
    }

    public bool IsChecked
    {
        get { return Accessory == UITableViewCellAccessory.Checkmark; }
        set { Accessory = value ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.None; }
    }

    public static TaxaListCellView Create ()
    {
        return (TaxaListCellView)Nib.Instantiate (null, null) [0];
    }

    public override void SetSelected (bool selected, bool animated)
    {
        Accessory = selected ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.None;

        base.SetSelected (selected, animated);
    }
}

I'm not sure if there is anything wrong with my MvxTableViewController but here's the code

public class TaxaListView : MvxTableViewController
{
    public TaxaListView()
    {
        Title = "Taxon List";
    }

    private UISearchBar _searchBar;


    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        _searchBar = new UISearchBar(new RectangleF(0,0,320, 44))
        {
            AutocorrectionType = UITextAutocorrectionType.Yes,                
        };
        _searchBar.SearchButtonClicked += SearchBar_SearchButtonClicked;
        _searchBar.TextChanged += SearchBarOnTextChanged;

        var source = new MvxSimpleTableViewSource(TableView, TaxaListCellView.Key, TaxaListCellView.Key);

        var set = this.CreateBindingSet<TaxaListView, TaxaListViewModel> ();
        set.Bind (source).To (vm => vm.Taxa);
        set.Bind (source)
        .For (s => s.SelectionChangedCommand)
        .To (vm => vm.ItemSelectedCommand);

        set.Apply ();

        TableView.RowHeight = 50;
        TableView.Source = source;
        TableView.AllowsSelection = true;
        TableView.AllowsSelectionDuringEditing = true;
        TableView.TableHeaderView = _searchBar;

        TableView.ReloadData();
    }

    private void SearchBarOnTextChanged(object sender, UISearchBarTextChangedEventArgs uiSearchBarTextChangedEventArgs)
    {
        if(string.IsNullOrWhiteSpace(_searchBar.Text))
        {
            ((TaxaListViewModel) ViewModel).SearchTaxaByText(string.Empty);
        }
    }

    void SearchBar_SearchButtonClicked(object sender, System.EventArgs e)
    {   
        ((TaxaListViewModel)ViewModel).SearchTaxaByText(_searchBar.Text);
    }
}

When I Select an item from the list first When I Select an item from the list first When i start searching, Or happens even if i go back into the list of items When I start searching, Or happens even if I go back into the List of items

Was it helpful?

Solution

As Stuart alluded too, I needed to tell the ViewModel the value had changed. I removed the SetSelelted method as this was causing problems when the cell was loading

public partial class TaxaListCellView : MvxTableViewCell
{
    public static readonly UINib Nib = UINib.FromName ("TaxaListCellView", NSBundle.MainBundle);
    public static readonly NSString Key = new NSString ("TaxaListCellView");

    private const string BindingText = "Name Name; IsChecked IsSelected";

    public TaxaListCellView() : base(BindingText)
    {
        Accessory = UITableViewCellAccessory.Checkmark;
        SelectionStyle = UITableViewCellSelectionStyle.None;

    }

    public TaxaListCellView (IntPtr handle) : base (BindingText,handle)
    {
        Accessory = UITableViewCellAccessory.Checkmark;
        SelectionStyle = UITableViewCellSelectionStyle.None;
    }

    public string Name
    {
        get { return lblSelectedTaxon.Text; }
        set { lblSelectedTaxon.Text = value; }
    }

    public bool IsChecked
    {
        get { return Accessory == UITableViewCellAccessory.Checkmark; }
        set { Accessory = value ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.None; }
    }

    public static TaxaListCellView Create ()
    {
        return (TaxaListCellView)Nib.Instantiate (null, null) [0];
    }
}  

In My TaxaListView class

 public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        _searchBar = new UISearchBar(new RectangleF(0,0,320, 44))
        {
            AutocorrectionType = UITextAutocorrectionType.Yes,                
        };
        _searchBar.SearchButtonClicked += SearchBar_SearchButtonClicked;
        _searchBar.TextChanged += SearchBarOnTextChanged;

        var source = new MvxSimpleTableViewSource(TableView, TaxaListCellView.Key, TaxaListCellView.Key);

        var set = this.CreateBindingSet<TaxaListView, TaxaListViewModel> ();
        set.Bind (source).To (vm => vm.Taxa);
        set.Bind (source)
        .For (s => s.SelectionChangedCommand)
        .To (vm => vm.ItemSelectedCommand);

        set.Apply ();

        TableView.RowHeight = 50;
        TableView.Source = source;
        TableView.AllowsSelection = true;
        TableView.AllowsSelectionDuringEditing = true;
        TableView.TableHeaderView = _searchBar;

        TableView.ReloadData();
    }

I Bind the selectedChangedCommand and in my ViewModel class I raise the Property changed event

private MvxCommand<TaxonViewModel> _itemSelectedCommand;

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

    private void DoSelectedItem(TaxonViewModel item)
    {
        Taxa.First(r => r.TaxonId == item.TaxonId).IsSelected = true;

        RaisePropertyChanged("Taxon");
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top