可用的 v3 表源有:
抽象类
- MvxBaseTableViewSource
- 仅基本功能
- no
ItemsSource
- 一般不直接使用
- MvxTableViewSource.cs
- 从基表继承并添加
ItemsSource
数据绑定
- 继承类只需要实现
protected abstract UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item);
具体类
一般我使用:
- 在演示中:
- 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