将选中标记添加到所选项目后如何更新表格视图?

时间:2019-01-15 14:31:44

标签: c# ios xamarin mvvmcross checkmark

我正在Xamarin MvvmCross项目中的iOS视图中创建清单。一切正常,除了在选中一个项目时显示一个选中标记,并在取消选中一个项目时删除选中标记。

在我控制器的ViewDidLoad内部:

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();

我尝试调用ReloadData,但数据未更改,仅UI更改,我也尝试在GetCell中设置选中标记,但与RowSelected内部存在相同问题

以及表源代码:

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();


            }

        }          

    }


}

控制器代码:

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

}

绑定工作正常,我可以捕获单击/选定的项目,但是视图没有更新(我使用xcode模拟器)。欢迎任何建议!

2 个答案:

答案 0 :(得分:0)

您可以尝试与此交换表源代码:

public class EntryTypesTableSource : MvxTableViewSource
{
    private bool _isFirst = true;
    private NSIndexPath _previousSelectedRow = null;

    public HomeTableViewSource(UITableView tableView) : base(tableView)
    {
        tableView.RegisterNibForCellReuse(EntryTypeCell.Nib, EntryTypeCell.Key);

        tableView.AllowsMultipleSelection = false;
        tableView.AllowsSelection = true;
    }

    protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
    {
        var cell = tableView.DequeueReusableCell(EntryTypeCell.Key, indexPath);

        if (_isFirst)
        {
            cell.Accessory = UITableViewCellAccessory.Checkmark;
            _previousSelectedRow = indexPath;
            _isFirst = false;
        }

        return cell;
    }

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {
        if (_previousSelectedRow == indexPath)
            return;

        base.RowSelected(tableView, indexPath);

        var cell = tableView.CellAt(indexPath);
        cell.Accessory = UITableViewCellAccessory.Checkmark;
        var previousSelectedCell = tableView.CellAt(_previousSelectedRow);
        previousSelectedCell.Accessory = UITableViewCellAccessory.None;

        _previousSelectedRow = indexPath;
    }
}

然后,您应该在视图控制器中更新

var source = new EntryTypesTableSource(eventsDialogTable, EntryTypeCell.Key, EntryTypeCell.Key);

进入

var source = new EntryTypesTableSource(eventsDialogTable);

您还可以尝试的一件事是将RowSelected方法更新为:

public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
    if (_previousSelectedRow == indexPath)
        return;

    base.RowSelected(tableView, indexPath);

    var cell = tableView.CellAt(indexPath);
    var previousSelectedCell = tableView.CellAt(_previousSelectedRow);

    InvokeOnMainThread(() =>
    {
        cell.Accessory = UITableViewCellAccessory.Checkmark;
        previousSelectedCell.Accessory = UITableViewCellAccessory.None;
    });

    _previousSelectedRow = indexPath;
}

答案 1 :(得分:0)

在设备上运行应用后,一切正常。是模拟器问题。我保留了代码的原始版本。谢谢大家的帮助和时间