gpt4 book ai didi

ios - 使用 Mvvmcross 将分组数据绑定(bind)到 Xamarin 中的 UITableview

转载 作者:行者123 更新时间:2023-12-01 18:10:08 25 4
gpt4 key购买 nike

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

首先,我创建我的表:

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

然后我创建我的 View 源并绑定(bind)它:
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();

我现在正在使用一个简化的类(class);所以绑定(bind)目标(TableSourceItem)当前定义为:
public ObservableCollection<Stuff> TableSourceItem
{
get
{
return tableSourceItem;
}

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

而“东西”类很简单
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);

代码的最后一点,是我定义的表格 View 源。这继承自 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 的一个部分类(class)。

有任何想法吗?

最佳答案

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

protected override object GetItemAt(NSIndexPath indexPath)
{

if (items == null)
{
return null;
}

return items[indexPath.Section].Items.ElementAt((int)indexPath.Item);
}

一旦我包含了这个方法覆盖它就可以正常工作!

关于ios - 使用 Mvvmcross 将分组数据绑定(bind)到 Xamarin 中的 UITableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33084740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com