Reputation: 51
I see wonderful examples of databinding to templates using MVVMCross. However, I have complex datasources that sets cell UI types in the GetCell(ios & Android) based on properties of the item in the collection currently being loaded:
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;
}
}
How would you accomplish this in MVVMCross with binding or using templates as described in examples posted illustrating binding to lists with templates?
Upvotes: 0
Views: 975
Reputation: 66882
Polymorphic listviews in Android, iOS and Windwos Phone are shown in https://github.com/MvvmCross/MvvmCross-Samples/tree/master/WorkingWithCollections
In iOS, this sample uses a custom data source which switches identifier based on the individual item:
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);
}
Upvotes: 2