无法绑定到mvvmcross中的ios表节单元,子单元按预期方式绑定 [英] Unable to bind to ios table section cells in mvvmcross, child cells bind as expected

查看:47
本文介绍了无法绑定到mvvmcross中的ios表节单元,子单元按预期方式绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建具有分组节的TableView,如下所示:

I'm trying to create a TableView with grouped sections like so:

我将组列表绑定到表.一个组包含一些字符串属性和一个项目列表,而一个项目也包含一些字符串属性.组和项目单元格都有自己的类,扩展了MvxTableViewCell.

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.

在类中,我要绑定的每个单元格如下:

Inside the class for each the cells I am binding as follows:

项目单元格:

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

还有组单元格:

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

主视图中的My TableSource扩展了MvxStandardTableViewSource并使用重写的方法来获取表的各个单元格. public override UIView GetViewForHeader返回组单元格视图,而protected override UITableViewCell GetOrCreateCellFor返回项目单元格视图.

My TableSource in the main view extends MvxStandardTableViewSource and uses the overridden methods to get the respective cells for the table. public override UIView GetViewForHeader returns a group cell view while protected override UITableViewCell GetOrCreateCellFor returns an item cell view.

我的问题是,即使我看不到两者之间没有明显区别,组视图单元格也不会绑定,而项目视图单元格则绑定得很好.但是,正在构建组视图单元,只是绑定没有在单元内部发生.

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.

添加了表格视图源:

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

推荐答案

我认为您的解决方案已经存在-但您需要提供一个GetViewForHeader实现,该实现有点像绑定GetCell实现, MvvmCross提供-请注意以下位置的DataContext设置:

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:

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

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

对于标题,您需要将DataContext设置为组标题的相应视图模型".

For the header, you'll need to set the DataContext to the appropriate "view model" for the group header.

这篇关于无法绑定到mvvmcross中的ios表节单元,子单元按预期方式绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆