Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I provide GetSizeForItem implementation in UICollectionViewController?

UICollectionViewDelegateFlowLayout has a method called sizeForItem (GetSizeForItem in MonoTouch).

But I'm not providing the delegate explicitly—instead, I'm inheriting from UICollectionViewController.
It mixes data source ands delegate functionality but doesn't have this method to override.

I tried adding this to my controller:

[Export ("collectionView:layout:sizeForItemAtIndexPath:")]
public virtual SizeF GetSizeForItem (UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
{
    return new SizeF (100, 100);
}

and it was never called.

How do I provide this method without resorting to separating delegate and data source?

like image 462
Dan Abramov Avatar asked Dec 09 '12 22:12

Dan Abramov


2 Answers

You can't. In Obj-C the viewcontroller (or any class) object can adopt the delegate protocol. This is not posible in Monotouch. You gave to use a delegate instance. But this can be a private class

public class CustomCollectionViewController:UICollectionViewController
{
    public CustomCollectionViewController():base()
    {

        this.CollectionView.Delegate = new CustomViewDelegate();

    }

    class CustomViewDelegate: UICollectionViewDelegateFlowLayout
    {

        public override System.Drawing.SizeF GetSizeForItem (UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
        {
            return new System.Drawing.SizeF (100, 100);
        }
    }
}
like image 135
svn Avatar answered Oct 01 '22 13:10

svn


Edit: Without the need of creating a delegate subclass, add this in your UICollectionviewSource

/** Other methods such as GetItemsCount(), GetCell()... goes here **/

[Export ("collectionView:layout:sizeForItemAtIndexPath:"), CompilerGenerated]
public virtual CGSize GetSizeForItem (UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
{
    return new CGSize (width, height);
}
like image 23
Iain Smith Avatar answered Oct 01 '22 13:10

Iain Smith