使用Mvvmcross将分组数据绑定到Xamarin中的UITableview

时间:2015-10-12 15:24:17

标签: ios xamarin mvvmcross

我正在尝试将分组数据绑定到表中,但是当我运行代码时,绑定似乎试图为Section级别项创建一个单元格,而不是创建一个包含其背后单元格的部分。

首先,我创建了我的表:

    table = new UITableView(CGRect.Empty, UITableViewStyle.Grouped);
    table.RowHeight = UxConstants.UiDimensions.Dimension48;
    table.AutoresizingMask = UIViewAutoresizing.All;
    table.ScrollEnabled = true;

然后我创建我的视图源并绑定它:

var source = new OperationalInfoTableViewSource(table);
        table.Source = source;

        var set = this.CreateBindingSet<IncidentInformationView, IncidentInformationViewModel>();
        set.Bind(source).For(vm => vm.ItemsSource).To(vm => vm.TableSourceItem);
        set.Apply();

我现在正在使用简化课程;所以绑定目标(TableSourceItem)当前定义为:

public ObservableCollection<Stuff> TableSourceItem
{
    get
    {
        return tableSourceItem;
    }

    set
    {
        tableSourceItem = value;
        RaisePropertyChanged(() => TableSourceItem);
    }
}

和'Stuff'类只是

public class Stuff
{
    public string Title {
        get;
        set;
    }

    public List<string> Items {
        get;
        set;
    }
}

当我申报数据时:

        var items = new List<Stuff>();
        items.Add(new Stuff { Title = "Group 1", Items = new List<string> { "Item 1", "Item 2" } });
        items.Add(new Stuff { Title = "Group 2", Items = new List<string> { "Item 4", "Item 5"} });
        TableSourceItem = new ObservableCollection<Stuff>(items);

Code的最后一位是我定义的表视图源。这继承自MvxStandardTableView源:

    public class OperationalInfoTableViewSource : MvxStandardTableViewSource
    {
        List<Stuff> items;

        public OperationalInfoTableViewSource(UITableView tableView) : base (tableView)


   {
        TableView.RegisterClassForCellReuse (typeof(OperationalInfoViewCell), OperationalInfoViewCell.Key);
    }

    public override System.Collections.IEnumerable ItemsSource
    {
        get
        {
            return items;
        }
        set
        {
            if (value != null)
            {
                items = new List<Stuff>();
                foreach (Stuff item in value)
                {
                    items.Add(item);
                }

                base.ItemsSource = items;
            }
        }
    }

    public override string TitleForHeader(UITableView tableView, nint section)
    {
        return "Header";
    }

    public override nint NumberOfSections(UITableView tableView)
    {
        var i = items == null ? 0 : items.Count();

        Mvx.Trace(String.Format( "Number of Sections {0}",  i ));
        return i;
    }

    public override nint RowsInSection(UITableView tableview, nint section)
    {
        return items == null ? 0 : items[(int)section].Items.Count();
    }

    public override bool CanEditRow(UITableView tableView, NSIndexPath indexPath)
    {
        return false;
    }

    public override bool CanPerformAction(UITableView tableView, ObjCRuntime.Selector action, NSIndexPath indexPath, NSObject sender)
    {
        return false;
    }

    public override bool CanMoveRow(UITableView tableView, NSIndexPath indexPath)
    {
        return false;
    }       

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

        return cell;
    }

    public override nfloat GetHeightForHeader(UITableView tableView, nint section)
    {
        return UxConstants.UiDimensions.Dimension32;
    }

    public override UIView GetViewForHeader(UITableView tableView, nint section)
    {
        var operationalInfoTitle = new UILabel()
        {
            Lines = 1,
            Font = UxConstants.UxFonts.MediumBold,
            TextColor = UxConstants.UxColours.BBlue,
            Text = "Guidance & Info"
        };

        var operationalInfoTitleDivider = new UIView()
        { 
            BackgroundColor = UxConstants.UxColours.MGrey 
        };

        var operationalInfoListView = new UIView();

        operationalInfoListView.AddSubviews(operationalInfoTitle, operationalInfoTitleDivider);
        operationalInfoListView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();

        operationalInfoListView.AddConstraints(operationalInfoTitle.FullWidthOf(operationalInfoListView, UxConstants.UiDimensions.Dimension16));
        operationalInfoListView.AddConstraints(operationalInfoTitleDivider.FullWidthOf(operationalInfoListView, UxConstants.UiDimensions.Dimension16));

        operationalInfoListView.AddConstraints(
            operationalInfoTitle.Height().EqualTo(UxConstants.UiDimensions.Dimension32),
            operationalInfoTitle.AtTopOf(operationalInfoListView),
            operationalInfoTitleDivider.Below(operationalInfoTitle),
            operationalInfoTitleDivider.Height().EqualTo(2));

        return operationalInfoListView;
    }
}

我希望有2个部分,每个部分有两个单元格,但它似乎每个Stuff类创建一个单元格,而不是每个Stuff类创建一个单元格。

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

发现问题,我未能实现重要的覆盖:

[object, object, object, object, object]

一旦我加入了这个方法覆盖它就可以了!