gpt4 book ai didi

ios - MvvmCross 绑定(bind)到 Storyboard 中的表格单元格

转载 作者:行者123 更新时间:2023-12-01 17:39:42 25 4
gpt4 key购买 nike

我正在使用 MvvmCross 开发一个应用程序,它使用 Storyboard作为 UI。我没有使用 XIB 文件作为表格单元格,而是尝试从 Storyboard 中创建单元格。 Storyboard生成单元格的类,然后在我绑定(bind)到我的模型的那个类中。正在创建单元格,但在绑定(bind)到单元格中的每个标签时会出现以下错误,其中“MyStringProperty”是我尝试绑定(bind)到的属性的名称(名称、电话号码或电子邮件):

Failed to create target binding for binding Text MyStringProperty

该表的创建方式与 Stuart 的 Kitten Cell 教程中的相同,除了我使用以下代码行代替 XIB 来表示使用生成的单元格类:
tableView.RegisterClassForCellReuse(typeof(MyCell), MyCell.Key);

单元格中数据的绑定(bind)也采用与 Stuart 的 Kitten Cell 教程中相同的方式完成。

这是 MyCell 的代码
public partial class MyCell : MvxTableViewCell
{
public static readonly NSString Key = new NSString("MyCell");

public MyCell(IntPtr handle) : base(handle)
{
this.DelayBind(() =>
{
var set = this.CreateBindingSet<MyCell, ContactModel>();
set.Bind(myLabelOne).To(contact => contact.Name);
set.Bind(myLabelTwo).To(contact => contact.PhoneNumber);
set.Bind(myLabelThree).To(contact => contact.Email);
set.Apply();
});
}
}

设计师:
[Register ("MyCell")]
partial class MyCell
{
[Outlet]
MonoTouch.UIKit.UILabel myLabelOne{ get; set; }

[Outlet]
MonoTouch.UIKit.UILabel myLabelTwo{ get; set; }

[Outlet]
MonoTouch.UIKit.UILabel myLabelThree{ get; set; }

void ReleaseDesignerOutlets ()
{
if (myLabelOne!= null) {
myLabelOne.Dispose ();
myLabelOne= null;
}

if (myLabelTwo!= null) {
myLabelTwo.Dispose ();
myLabelTwo= null;
}

if (myLabelThree!= null) {
myLabelThree.Dispose ();
myLabelThree= null;
}
}
}

最佳答案

我遇到了几乎相同的问题,但我找到了解决方案:

此处的解决方案(我的问题 - 编辑很重要):https://stackoverflow.com/questions/24507192/mvvmcross-ios-storyboard-table-binding-not-working

明显流畅的装订和 StoryboardTableViewCell s 不能很好地协同工作。 CollectionViewCell 也是如此。 s(最终相同的解决方案,但您不需要自定义 MvxCollectionViewSource - 标准很好)

@stuart 可能更清楚为什么这两种方法会产生不同的结果

将您的单元格更改为:

public partial class MyCell : MvxTableViewCell
{
public static readonly NSString Key = new NSString("MyCell");
private const string BindingText = @"
MyLabelOneText Name;
MyLabelTwoText PhoneNumber;
MyLabelThreeText EMail";

public string MyLabelOneText
{
get { return myLabelOne.Text; }
set { myLabelOne.Text = value; }
}

public string MyLabelTwoText
{
get { return myLabelTwo.Text; }
set { myLabelTwo.Text = value; }
}

public string MyLabelThreeText
{
get { return myLabelThree.Text; }
set { myLabelThree.Text = value; }
}

public MyCell(IntPtr handle) : base(BindingText, handle)
{
}
}

您可能还想使用自定义 TableViewSource(或覆盖单元格中必要的构造函数):
public class MyTableViewSource : MvxTableViewSource
{
private NSString cellIdentifier = new NSString("MyCell");

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

protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
{
return (MyCell)tableView.DequeueReusableCell(cellIdentifier);
}
}

满足您的需求,唯一重要的部分是对基本构造函数的调用 MvxTableViewCell(bindingText, handle)
如果需要,我可以将项目上传到 GitHub

关于ios - MvvmCross 绑定(bind)到 Storyboard 中的表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24362702/

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