我看到使用MVVMCross对模板进行数据绑定的精彩示例。但是,我有复杂的数据源,可以根据当前正在加载的集合中的项目属性在GetCell(ios和Android)中设置单元格UI类型:
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
UITableViewCell cell = null;
var item = cellItems [indexPath.Section] [indexPath.Row];
switch (item.DisplayType) {
case DetailType.Name:
cell = tableView.DequeueReusableCell (cellIdentifier + "_name")as NameEditCell;
if (cell == null) {
cell = NameEditCell.Create ();
}
((NameEditCell)cell).BindFields (item);
break;
case DetailType.Phone:
cell = tableView.DequeueReusableCell (cellIdentifier + "_phone")as PhoneEditCell;
if (cell == null) {
cell = PhoneEditCell.Create ();
}
((PhoneEditCell)cell).BindFields (item);
break;
case DetailType.Email:
cell = tableView.DequeueReusableCell (cellIdentifier + "_email")as EmailEditCell;
if (cell == null) {
cell = EmailEditCell.Create ();
}
((EmailEditCell)cell).BindFields (item);
break;
case DetailType.Property:
cell = tableView.DequeueReusableCell (cellIdentifier + "_property")as PropertyEditCell;
if (cell == null)
cell = PropertyEditCell.Create ();
((PropertyEditCell)cell).BindFields (item);
break;
case DetailType.Address:
cell = tableView.DequeueReusableCell (cellIdentifier + "_address")as AddressEditCell;
if (cell == null) {
cell = AddressEditCell.Create ();
}
((AddressEditCell)cell).BindFields (item);
break;
}
return cell;
}
}
如何通过绑定或使用模板在MVVMCross中完成此操作,如发布的示例中所述,说明了绑定到带模板的列表?
答案 0 :(得分:2)
Android,iOS和Windwos Phone中的多态列表视图显示在https://github.com/MvvmCross/MvvmCross-Samples/tree/master/WorkingWithCollections
中在iOS中,此示例使用自定义数据源,该数据源根据单个项目切换标识符:
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);
}