简体   繁体   English

无法绑定到mvvmcross中的ios表节单元,子单元按预期方式绑定

[英]Unable to bind to ios table section cells in mvvmcross, child cells bind as expected

I'm trying to create a TableView with grouped sections like so: 我正在尝试创建具有分组部分的TableView,如下所示:

具有部分的TableView

I am binding a list of groups to the table. 我将组列表绑定到表。 A group contains a few string properties and a list of items, while an item contains some string properties as well. 一个组包含一些字符串属性和一个项目列表,而一个项目也包含一些字符串属性。 The group and item cells each have their own class which extends MvxTableViewCell . 组和项目单元格都有各自的类,这些类扩展了MvxTableViewCell

Inside the class for each the cells I am binding as follows: 在类中,我要绑定的每个单元格如下:

The item cell: 项目单元格:

 this.DelayBind(() =>
                {
                    var set = this.CreateBindingSet<ItemCellView, ItemCellViewModel>();
                    set.Bind(lblOne).To(item=> item.propertyOne);
                    set.Apply();
                });

And the group cell: 和组单元格:

this.DelayBind(() =>
                    {
                        var set = this.CreateBindingSet<GroupCellView, GroupCellViewModel>();
                        set.Bind(lblOne).To(group=> group.propertyOne);
                        set.Apply();
                    });

My TableSource in the main view extends MvxStandardTableViewSource and uses the overridden methods to get the respective cells for the table. 我在主视图中的MvxStandardTableViewSource扩展了MvxStandardTableViewSource并使用重写的方法来获取表的各个单元格。 public override UIView GetViewForHeader returns a group cell view while protected override UITableViewCell GetOrCreateCellFor returns an item cell view. public override UIView GetViewForHeader返回组单元格视图,而protected override UITableViewCell GetOrCreateCellFor返回项目单元格视图。

My problem is that the group view cell does not bind whatsoever while the item view cell binds just fine even though I see no discernible difference between the two. 我的问题是,即使我看不到两者之间没有明显的区别,组视图单元格也不会绑定,而项目视图单元格则绑定得很好。 The group view cell is being constructed however, it's just the binding that is not taking place inside the cell. 但是,正在构建组视图单元,只是单元内部没有发生绑定。

Edit: Added the table view source: 编辑:添加了表格视图源:

private class TableSource : MvxStandardTableViewSource
        {
            private List<GroupCellViewModel> _groups;
            public new List<GroupCellViewModel> ItemsSource
            {
                get
                {
                    return _groups;
                }
                set
                {
                    _groups = value;
                    ReloadTableData();
                }
            }

            public TableSource(UITableView tableView)
                : base(tableView)
            {
            }

            public override string TitleForHeader(UITableView tableView, int group)
            {
                if (_groups == null)
                    return string.Empty;

                return Utils.FormatDate(_groups[group].Date);
            }

            public override UIView GetViewForHeader(UITableView tableView, int group)
            {
                return MakeHeaderRow(tableView, _groups[group]);
            }

            public override float GetHeightForHeader(UITableView tableView, int section)
            {
                return 50;
            }

            public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
            {
                return 50;
            }

            public override int NumberOfSections(UITableView tableView)
            {
                if (_groups == null)
                    return 0;

                return _groups.Count;
            }

            public override int RowsInSection(UITableView tableview, int group)
            {
                if (_groups == null)
                    return 0;

                return _groups[group].Items.Count;
            }

            protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
            {
                UITableViewCell cell;
                if (item is Item)
                {
                    cell = MakeItemRow(tableView, (Item)item);
                }
                else
                {
                    throw new ArgumentException("Unknown cell type " + item.GetType().Name);
                }

                return cell;
            }

            protected override object GetItemAt(NSIndexPath indexPath)
            {
                if (_groups == null)
                    return null;

                return _groups[indexPath.Section].Items[indexPath.Row];
            }

            private UITableViewCell MakeHeaderRow(UITableView tableView, GroupCellViewModel group)
            {
                var existing = (GroupCellView)tableView.DequeueReusableCell(GroupCellView.Key);
                if (existing != null)
                    return existing;

                return new GroupCellView();
            }

            private UITableViewCell MakeItemRow(UITableView tableView, Item item)
            {
                var existing = (ItemCellView)tableView.DequeueReusableCell(ItemCellView.Key);
                if (existing != null)
                    return existing;

                return new ItemCellView();
            }
        }

I think your solution is almost there - but you'll need to provide a GetViewForHeader implementation which is a bit more like the binding GetCell implementation which MvvmCross provides - note the DataContext setting in: 我认为您的解决方案已经存在-但您需要提供一个GetViewForHeader实现,该实现有点类似于MvvmCross提供的绑定GetCell实现-请注意以下内容中的DataContext设置:

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var item = GetItemAt(indexPath);
        var cell = GetOrCreateCellFor(tableView, indexPath, item);

        var bindable = cell as IMvxDataConsumer;
        if (bindable != null)
            bindable.DataContext = item;

        return cell;
    }

from https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Touch/Views/MvxBaseTableViewSource.cs#L97 来自https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Touch/Views/MvxBaseTableViewSource.cs#L97

For the header, you'll need to set the DataContext to the appropriate "view model" for the group header. 对于标题,您需要将DataContext设置为组标题的适当“视图模型”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM