5

我正在使用 Xamarin 和 mvvmcross 以及最终绑定到可观察集合的表的视图。

视频提供了有关如何创建自定义单元格的非常丰富的信息,但似乎已过时。在大约 42 分钟时,Stuart 为他的表创建了一个数据源,该数据源派生自MvxSimpleBindableTableSource,但该类似乎不存在,或者至少我找不到它。那么使用 mvvmcross 绑定到 UITableView 的“最佳”方式是什么?

另外,我在常规 MvxViewController 中使用 UITableView,因为我似乎无法让 MvxTableViewController 与 xib 一起使用,这个问题似乎表明目前不可能。

4

1 回答 1

15

可用的 v3 表源有:

抽象类

  • MvxBaseTableViewSource
    • 仅基本功能
    • no ItemsSource- 一般不直接使用
  • MvxTableViewSource.cs
    • 从基表继承并添加ItemsSource数据绑定
    • 继承类只需要实现protected abstract UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item);

具体类

  • MvxStandardTableViewSource.cs
    • 继承自MvxTableViewSource
    • 通过提供“标准 iPhone 电池类型”UITableViewCellStyle
    • 在这些中你可以绑定TitleText, DetailText,ImageUrl和(带有一些戏弄)附件
  • MvxSimpleTableViewSource.cs
    • 继承自MvxTableViewSource
    • string nibName为集合中的所有项目提供单一单元格类型 - 通过ctor
    • 在这些单元格中,您可以绑定您喜欢的内容 - 观看视频(稍后)
  • MvxActionBasedTableViewSource.cs - 提供了一些Func<>样式钩子,允许您在GetOrCreateCellFor不继承新类的情况下实现MvxTableViewSource

一般我使用:

  • 在演示中:
    • a MvxStandardTableViewSource- 因为我无需创建自定义单元即可获得列表
  • 在实际代码中:
    • MvxSimpleTableViewSource我只需要一种细胞类型时
    • MvxTableViewSource当我需要多种细胞类型时继承的自定义类- 例如见下文

具有多种单元格类型的通用 TableSource 通常看起来像PolymorphicListItemTypesView.cs

public class PolymorphicListItemTypesView
    : MvxTableViewController
{
    public PolymorphicListItemTypesView()
    {
        Title = "Poly List";
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        var source = new TableSource(TableView);
        this.AddBindings(new Dictionary<object, string>
            {
                {source, "ItemsSource Animals"}
            });

        TableView.Source = source;
        TableView.ReloadData();
    }

    public class TableSource : MvxTableViewSource
    {
        private static readonly NSString KittenCellIdentifier = new NSString("KittenCell");
        private static readonly NSString DogCellIdentifier = new NSString("DogCell");

        public TableSource(UITableView tableView)
            : base(tableView)
        {
            tableView.RegisterNibForCellReuse(UINib.FromName("KittenCell", NSBundle.MainBundle),
                                              KittenCellIdentifier);
            tableView.RegisterNibForCellReuse(UINib.FromName("DogCell", NSBundle.MainBundle), DogCellIdentifier);
        }

        public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
        {
            return KittenCell.GetCellHeight();
        }

        protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath,
                                                              object item)
        {
            NSString cellIdentifier;
            if (item is Kitten)
            {
                cellIdentifier = KittenCellIdentifier;
            }
            else if (item is Dog)
            {
                cellIdentifier = DogCellIdentifier;
            }
            else
            {
                throw new ArgumentException("Unknown animal of type " + item.GetType().Name);
            }

            return (UITableViewCell) TableView.DequeueReusableCell(cellIdentifier, indexPath);
        }
    }
}

该视频提供了有关如何创建自定义单元格的非常丰富的信息,但似乎已过时

它是在 Xamarin 2.0 和 V3 之前制作的,但原理非常相似。

该文章的代码已更新 - 请参阅https://github.com/slodge/MvvmCross-Tutorials/tree/master/MonoTouchCellTutorial

除此之外:

另外,我在常规 MvxViewController 中使用 UITableView,因为我似乎无法让 MvxTableViewController 与 xib 一起使用,这个问题似乎表明目前不可能。

我认为这已经得到修复 - 请参阅MvxTableViewController.cs#L33

于 2013-06-07T14:23:45.553 回答