Thursday, January 24, 2013

Work In Progress - MvvmCross lists sample

I'm working on some new sample code (slowly!)


A preview of this content - with very bad audio - is at: http://screencast.com/t/foV2Eoj6

I will blog more fully about this soon... and I will add some CollectionView as well as TableView code... but in the meantime, here's a screenshot and the code behind it:


using System;
using Collections.Core.ViewModels;
using Cirrious.MvvmCross.Touch.Views;
using Cirrious.MvvmCross.Views;
using Cirrious.MvvmCross.Binding.Touch.ExtensionMethods;
using Cirrious.MvvmCross.Binding.Touch.Views;
using System.Collections.Generic;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using Collections.Core.ViewModels.Samples.SmallFixed;
using Cirrious.MvvmCross.Interfaces.ViewModels;
using Collections.Core.ViewModels.Samples.ListItems;
using Collections.Core.ViewModels.Samples.MultipleListItemTypes;
namespace Collections.Touch
{
public class PolymorphicListItemView
: MvxBindingTouchTableViewController<PolymorphicListItemTypesViewModel>
{
public PolymorphicListItemView (MvxShowViewModelRequest request)
: base(request)
{
Title = "Poly List";
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
var source = new TableSource(TableView);
this.AddBindings(new Dictionary<object, string>()
{
{source, "{'ItemsSource':{'Path':'Animals'}}" }
});
TableView.Source = source;
TableView.ReloadData();
}
public class TableSource : MvxSimpleBindableTableViewSource
{
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);
}
}
}
}

Special thanks due to Mike Bluestein for his Xaminar on iOS6 and UICollectionViews - I learnt a lot watching and listening to it - http://dotnetmobilepodcast.blogspot.co.uk/2012/11/introduction-to-collection-views-in-ios.html - and need to watch it a few more times yet!

No comments:

Post a Comment