title: UICollectionView - Curious flow layout
body: <p>I'm experiencing with the UICollectionView(Controller) for the first time. Actually it should be as simple as working with TableViews, it's not though.</p>
  
  <p>Rather than showing all images in a flow (several rows) just the top row is displayed. All the other images are somewhere... scrolling is enabled but nothing happens, no bouncing, no scrolling, ...
  And after an orientation change (and back) some more images are visible but they appear randomly. After every orientation change they appear at an other location.</p>
  
  <p>My example should have 7 images.</p>
  
  <p>My properties in IB:
  <img src="https://i.stack.imgur.com/xCt9B.png" alt="IB Settings Screenshot"></p>
  
  <p>First time:
  <img src="https://i.stack.imgur.com/gBoEx.png" alt="First time"></p>
  
  <p>After rotating (and back):
  <img src="https://i.stack.imgur.com/4JIWR.jpg" alt="After rotating"></p>
  
  <p>And my source code to implement the photo gallery.</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  
  using MonoTouch.Foundation;
  using MonoTouch.UIKit;
  using System.Collections.Generic;
  using Xamarin.Media;
  using MonoTouch.AssetsLibrary;
  using MonoTouch.CoreGraphics;
  using System.Diagnostics;
  using System.Linq;
  using System.Drawing;
  
  namespace B2.Device.iOS
  {
      public partial class TagesRapportDetailRegieBilderCollectionViewController : UICollectionViewController
      {
          private const string Album = "Rapport";
  
          public TagesRapportDetailRegieBilderSource Source { get; private set; }
          private TagesRapportDetailRegieBilderDelegate _delegate;
  
          public TagesRapportDetailRegieBilderCollectionViewController (IntPtr handle) : base (handle)
          {
              Source = new TagesRapportDetailRegieBilderSource(this);
              _delegate = new TagesRapportDetailRegieBilderDelegate(this);
  
              // Delegate - Muss im konstruktor sein. ViewDidLoad geht nicht!
              CollectionView.Delegate = _delegate;
          }
  
          public override void ViewDidLoad()
          {
              base.ViewDidLoad();
  
              // Cell Klasse registrieren
              CollectionView.RegisterClassForCell (typeof(ImageCell), new NSString("imageCell"));
  
              // DataSource
              CollectionView.Source = Source;
  
              // Bilder laden
              LoadImages();
          }
  
          private void LoadImages()
          {
              Source.Images.Clear();
  
              var assetsLibrary = new ALAssetsLibrary();
              assetsLibrary.Enumerate(ALAssetsGroupType.Album, GroupsEnumeration, GroupsEnumerationFailure);
          }
  
          private void GroupsEnumeration(ALAssetsGroup group, ref bool stop)
          {
              if (group != null &amp;&amp; group.Name == Album)
              {
                  //notifies the library to keep retrieving groups
                  stop = false;
  
                  //set here what types of assets we want,
                  //photos, videos or both
                  group.SetAssetsFilter(ALAssetsFilter.AllPhotos);
  
                  //start the asset enumeration
                  //with ALAssetsGroup's Enumerate method
                  group.Enumerate(AssetsEnumeration);
  
                  CollectionView.ReloadData();
              }
          }
  
          private void AssetsEnumeration(ALAsset asset, int index, ref bool stop)
          {
              if (asset != null)
              {
                  //notifies the group to keep retrieving assets
                  stop = false;
  
                  //use the asset here
                  var image = new UIImage(asset.AspectRatioThumbnail());
  
                  Source.Images.Add(image);
              }
          }
  
          private void  GroupsEnumerationFailure(NSError error)
          {
              if (error != null)
              {
                  new UIAlertView("Zugriff verweigert", error.LocalizedDescription, null, "Schliessen", null).Show();
              }
          }
      }
  
      public class TagesRapportDetailRegieBilderDelegate : UICollectionViewDelegateFlowLayout
      {
          private TagesRapportDetailRegieBilderCollectionViewController _controller;
  
          public TagesRapportDetailRegieBilderDelegate(TagesRapportDetailRegieBilderCollectionViewController controller)
          {
              _controller = controller;
          }
  
          public override System.Drawing.SizeF GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
          {
              var size = _controller.Source.Images[indexPath.Row].Size.Width &gt; 0
                  ? _controller.Source.Images[indexPath.Row].Size : new SizeF(100, 100);
  
              size.Width /= 3;
              size.Height /= 3;
  
              return size;
          }
  
          public override UIEdgeInsets GetInsetForSection(UICollectionView collectionView, UICollectionViewLayout layout, int section)
          {
              return new UIEdgeInsets(50, 20, 50, 20);
          }
      }
  
      public class TagesRapportDetailRegieBilderSource : UICollectionViewSource
      {
          private TagesRapportDetailRegieBilderCollectionViewController _controller;
  
          public List&lt;UIImage&gt; Images { get; set; }
  
          public TagesRapportDetailRegieBilderSource(TagesRapportDetailRegieBilderCollectionViewController controller)
          {
              _controller = controller;
              Images = new List&lt;UIImage&gt;();
          }
  
          public override int NumberOfSections(UICollectionView collectionView)
          {
              return 1;
          }
  
          public override int GetItemsCount(UICollectionView collectionView, int section)
          {
              return Images.Count;
          }
  
          public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
          {
              var cell = collectionView.DequeueReusableCell(new NSString("imageCell"), indexPath) as ImageCell;
  
              cell.Image = Images[indexPath.Row];
  
              return cell;
          }
      }
  
      public class ImageCell : UICollectionViewCell
      {
          UIImageView imageView;
  
          [Export ("initWithFrame:")]
          public ImageCell (System.Drawing.RectangleF frame) : base (frame)
          {
              imageView = new UIImageView(frame);
              imageView.AutoresizingMask = ~UIViewAutoresizing.None;
              ContentView.AddSubview (imageView);
          }
  
          public UIImage Image 
          {
              set 
              {
                  imageView.Image = value;
              }
          }
      }
  }
  </code></pre>