Search code examples
c#iosxamarinmvvmcrosscheckmark

How to update table view after adding checkmark to selected item?


I'm creating a checklist in a iOS view inside my Xamarin MvvmCross project. Everything works fine except showing a checkmark when an item is selected and removing the checkmark when an item is deselected.

Inside ViewDidLoad of my controller:

var source = new EntryTypesTableSource(eventsDialogTable, EntryTypeCell.Key, EntryTypeCell.Key);
eventsDialogTable.Source = source;
var set = this.CreateBindingSet<EventsDialogView,EventsDialogViewModel>();
set.Bind(source).To(vm => vm.SelectedItem.EntryTypes);
set.Apply();

I tried calling ReloadData but the data is not changed, only UI is changed, also I tried setting checkmark in GetCell but same problem as inside RowSelected

And code for table source:

public class EntryTypesTableSource : MvxSimpleTableViewSource
{       

    NSIndexPath selectedBefore;      

    public EntryTypesTableSource(UITableView tableView, string nibName, string cellIdentifier = null, NSBundle bundle = null) : base(tableView, nibName, cellIdentifier, bundle)
    {
        tableView.AllowsMultipleSelection = false;
        tableView.AllowsSelection = true;

    }


    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        UITableViewCell cell = base.GetCell(tableView, indexPath);  
        //Default first item is selected            
        if (indexPath.Row == 0 )
            {
                cell.Accessory = UITableViewCellAccessory.Checkmark;
                selectedBefore = indexPath;
            }


        return cell;           

    }


    public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {
        base.RowSelected(tableView, indexPath);



        if (indexPath != null)
        {

            UITableViewCell cellNow = tableView.CellAt(indexPath);//currently selected
            UITableViewCell cellOld = tableView.CellAt(selectedBefore); //previous                     



            if (selectedBefore != indexPath)
            {

                cellOld.Accessory = UITableViewCellAccessory.None;
                tableView.DeselectRow(selectedBefore, true);

                cellNow.Accessory = UITableViewCellAccessory.Checkmark;
                selectedBefore = indexPath;

                tableView.EndUpdates();


            }

        }          

    }


}

Controller code:

 public partial class EventsDialogView : MvxViewController<EventsDialogViewModel>
    {


        public EventsDialogView()
        {           

        }

        public override void ViewDidLoad()
        {
            // Releases the view if it doesn't have a superview.

            base.ViewDidLoad();

             //values
            var dialogWidth = 0.8 * this.View.Frame.Width;
            //float headerHeight = 50f;
            //float footerHeight = 50f;
            float rowHeight = 50f;
            int numberOfRows = selectedItem.EntryTypes.Count + 2;//+2 for header and footer
            float tableHeigth = numberOfRows * rowHeight;

            //table
            var eventsDialogTable = new UITableView();
            eventsDialogTable.Frame = new CoreGraphics.CGRect((this.View.Frame.Width - dialogWidth) / 2, (this.View.Frame.Height - tableHeigth) / 2, dialogWidth, tableHeigth);
            this.View.AddSubview(eventsDialogTable);

            var source = new EntryTypesTableSource(eventsDialogTable);
            eventsDialogTable.Source = source;        

            eventsDialogTable.SeparatorStyle = UITableViewCellSeparatorStyle.None;

            /*Binding*/
            var set = this.CreateBindingSet<EventsDialogView,EventsDialogViewModel>();            
            set.Bind(source).To(vm => vm.SelectedItem.EntryTypes);
            set.Bind(source).For(s => s.SelectionChangedCommand).To(vm => vm.EntrySelected);      
            set.Apply();          

        }  


    }

ViewModel:

public class EventsDialogViewModel : MvxViewModel
{
      private ObservableCollection<EntryType> entryTypeCollection = new ObservableCollection<EntryType>();
        private Device selectedItem;

    private EntryType selectedEntry;
      public EventsDialogViewModel(){}
       public ObservableCollection<EntryType>EntryTypeCollection
    {
        get { return entryTypeCollection; }
        set
        {
            entryTypeCollection = value;
            RaisePropertyChanged(() => EntryTypeCollection);
        }
    }

      public Device SelectedItem
    {
        get { return selectedItem; }
        set
        {
            selectedItem = value;
            RaisePropertyChanged(() => SelectedItem);
        }
    }

    public EntryType SelectedEntry
    {
        get { return selectedEntry; }
        set
        {
            selectedEntry = value;
            RaisePropertyChanged(() => SelectedEntry);
        }
    }

}

Binding works fine, and I can catch clicked/selected item, but view is not updating (I use xcode simulator). Any suggestion is welcome!


Solution

  • After I run app on device everything works fine. It was simulator problem. I kept original version of my code. Thanks everyone for help and time