Skip to content

Instantly share code, notes, and snippets.

@anujb
Created November 12, 2012 00:00
Show Gist options
  • Save anujb/4056794 to your computer and use it in GitHub Desktop.
Save anujb/4056794 to your computer and use it in GitHub Desktop.
UICollectionView in Practice
public class ProductViewController : UICollectionViewController {
ProductViewModel productViewModel;
public static NSString CellId = new NSString ("product_cell");
public ProductViewController () : base(new ProductCollectionLayout()) {
}
public override void ViewDidLoad () {
base.ViewDidLoad ();
productViewModel = new ProductViewModel();
CollectionView.RegisterClassForCell(typeof(ProductCell), CellId);
CollectionView.ScrollEnabled = true;
}
public override void ViewDidAppear (bool animated) {
base.ViewDidAppear(animated);
//
//.....Yo dawg, I herd u async, do it after the View renders...
//
productViewModel.GetOrderProductsAsync(ProductGroup.Id).ContinueWith((t) => {
BeginInvokeOnMainThread(() => {
this.CollectionView.ReloadData();
});
});
}
public override int GetItemsCount (UICollectionView collectionView, int section) {
if(productViewModel.OrderProducts != null){
return productViewModel.OrderProducts.Count;
}
return 0;
}
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath) {
var cell = (ProductCell)collectionView.DequeueReusableCell(CellId, indexPath);
var op = productViewModel.OrderProducts[indexPath.Row];
cell.OrderProduct = op;
cell.Initialize();
return cell;
}
public class ProductCell : UICollectionViewCell {
public Product Model { get; set; }
UILabel productLabel { get; set; }
[Export ("initWithFrame:")]
public ProductCell(RectangleF frame)
: base(frame) {
//Not able to overload constructor
}
public void Initialize() {
if(Model != null & Model != default(Product)) {
//
// ....Now ready to make use of Product model...
//
productLabel.Text = Model.Name;
this.ContentView.AddSubView(ProductLabel)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment