4

Stackoverflow question https://stackoverflow.com/a/13555814/1678391 was answered about setting variable sized cells in a UICollectionViewController. I'm trying to figure out how to do this with a UICollectionView object contained in a UIViewController. GetSizeForItem is what I need but don't see where to get it.

My implementation adds a UICollectionView to my UIViewController via Xcode. In my code I'm doing the following;

public partial class CustomViewController : UIViewController
{
    public override void ViewDidLoad()
    {
        collectionView.RegisterClassForCell(typeof(MyCell), cellId);
    }

    public override void ViewWillAppear(bool animated)
    {
        collectionView.Source = new CollectionViewSource(items, cellId);
    }

    class CollectionViewSource : UICollectionViewSource
    {
    }

    class MyCell : UICollectionViewCell
    {
    }
}
Community
  • 1
  • 1
Vincent
  • 155
  • 3
  • 11

1 Answers1

1

According to http://docs.go-mono.com GetSizeForItem is available only for UICollectionViewDelegateFlowLayout class.

To use that method you should implement UICollectionViewDelegateFlowLayout subclass and assing it to Delegate property of UICollectionViewController. Example.

Community
  • 1
  • 1
Maxim Korobov
  • 2,574
  • 1
  • 26
  • 44
  • 1
    I'm not using a UICollectionViewController though. Just UICollectionView within a UIViewController. Is setting the size only possible if using UICollectionViewController? Thanks – Vincent Mar 11 '13 at 14:11
  • You can (http://stackoverflow.com/questions/14291889/how-can-we-use-uicollectionview-without-an-uicollectionviewcontroller). Try to set second parameter in `UICollectionView`'s constructor or set `UICollectionView`'s `CollectionViewLayout` property later. – Maxim Korobov Mar 11 '13 at 14:29
  • Can't seem to get the delegate subclass to be called. flowLayoutDelegate = new CollectionViewDelegateFlowLayout(); lineLayout = new CollectionViewFlowLayout(); collectionView.RegisterClassForCell(typeof(MyCell), CellId); collectionView.CollectionViewLayout = lineLayout; collectionView.Delegate = flowLayoutDelegate; – Vincent Mar 12 '13 at 20:55
  • You created basic `UICollectionViewDelegateFlowLayout` class, which is using default values. Instead, you should to subclass `UICollectionViewDelegateFlowLayout` and override `GetSizeForItem` method. – Maxim Korobov Mar 13 '13 at 08:02
  • For demo purpose, I forked oficial SimpleCollectionView demo project (https://github.com/xamarin/monotouch-samples/tree/master/SimpleCollectionView) and implemented subclass `UICollectionViewDelegateFlowLayout`. See `CustomFlowLayoutDelegate` class implementation and usage in demo at https://bitbucket.org/MaximKorobov/uicollectionviewdelegateflowlayout-demo – Maxim Korobov Mar 13 '13 at 08:03
  • My code above is a subclassed `UICollectionViewDelegateFlowLayout`. The sample you show above is based on a `UICollectionViewController`. Mine is based on a `UICollectionView`. I've downloaded and run your sample. The delegate stuff is basically what I'm doing. I've added a view to your sample with what I'm doing. I've placed it on dropbox at (https://www.dropbox.com/s/90isdyt9er438xa/CollectionViewSample.zip). – Vincent Mar 13 '13 at 18:44
  • I am having the same issue. Were you able to solve it without using the UICollectionViewController? – perfect_element Nov 23 '13 at 23:14