1

Does anybody know how to subclass a UICollectionView in Xamarin.iOS?

I tried this

public class MyCustomUICollectionView : UICollectionView
{

    [Export ("initWithFrame:")]
    public InfinitiveScrollingUICollectionView (CGRect frame) : base(frame)
    {
        // initialization
    }
}

but I get

The best overloaded method match for UIKit.UICollectionView.UICollection(Foundation.NSCoder) has some invalid arguments Argument #1 cannot convertCoreGraphics.CGRectexpression to typeFoundation.NSCoder`.

I also tried to use public InfinitiveScrollingUICollectionView () but I get

The type UIKit.UICollectionView does not contain a constructor that takes '0' arguments

I want to override LayoutSubviews. Or should the UICollectionViewController be used for such a purpose?

testing
  • 19,681
  • 50
  • 236
  • 417

1 Answers1

3

UICollectionView has no constructor that accepts a single CGRect, so you have to pass a layout, too:

public class MyCustomUICollectionView : UICollectionView
{
    public MyCustomUICollectionView(CGRect frame, UICollectionViewLayout layout) 
        : base(frame, layout)
    {
    }
}

If you want to, you can also create the layout internally, so you don't have to pass it from the outside:

public class MyCustomUICollectionView : UICollectionView
{
    private static readonly UICollectionViewLayout _layout;

    static MyCustomUICollectionView()
    {
        // Just an example
        _layout = new UICollectionViewFlowLayout();
    }

    public MyCustomUICollectionView(CGRect frame) : base(frame, _layout)
    {
    }
}
asp_net
  • 3,567
  • 3
  • 31
  • 58
  • Do you know where I can look up the constructor? I tried to use the first approach you posted. I put this in the constructor of `UICollectionViewController`: `this.CollectionView = new MyCustomUICollectionView(this.View.Frame,layout)` but here he couldn't dequeue a cell. It does work in `ViewDidLoad` when I create a new layout. Shouldn't it work in the constructor also? – testing Mar 28 '15 at 21:02
  • 1
    See http://blog.thomasbandt.de/39/2434/de/blog/monotouch-viewdidload-gets-called-before-constructor.html – asp_net Mar 30 '15 at 09:35
  • When I instantiate my `UICollectionViewController` I have to pass a layout. Should I pass a fake layout or should I temporarily store the layout in a variable before I can pass it to `MyCustomUICollectionView`? – testing Mar 30 '15 at 09:50
  • 1
    If you want to instantiate layout and collection view later on, just don't use an UICollectionViewController, instead use a normal UIViewController and add a new UICollectionView. – asp_net Mar 30 '15 at 13:23
  • Ok. Who does get the method calls like `NumberOfSections`, `GetItemsCount` and `GetCell` then? Is it the collection view itself or do I have to define an `UICollectionViewDataSource`? – testing Mar 30 '15 at 13:26
  • You add the collection view as subview and a `UICollectionViewSource` has to be defined (includes `UICollectionViewDelegate` and `UICollectionViewDataSource`). – testing Mar 31 '15 at 07:07