7

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?

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
  • You can't. In Obj-C the datasource object can adopt the delegate protocol. This is not posible in Monotouch. – svn Dec 10 '12 at 10:53
  • @svn: So the only solution is to get rid of the controller and use data source and delegate instead? If this is the case, please post it as an answer so I could accept it. – Dan Abramov Dec 10 '12 at 11:49
  • You could define and assign you custom delegate and datasource inside your subclassed controller. This way you can just use your customcontroller form the outside and internally use your custom delegates – svn Dec 10 '12 at 12:00
  • Strangely, [exporting this method works for some people](http://forums.xamarin.com/discussion/comment/1747/#Comment_1747). – Dan Abramov Dec 10 '12 at 12:35
  • 1
    Sounds to my like a bug, It shoun't be in UICOllectionViewController according to the apple api. In other places in monotouch it is implementen as a separete delegate class (UITableView ect). My anwser below is the normal Monotouch way of doing it – svn Dec 10 '12 at 12:41

2 Answers2

13

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);
        }
    }
}
svn
  • 1,235
  • 10
  • 22
  • Note, though, that in this case methods like `ShouldSelectItem` and other delegate methods will be called on your delegate and **not** on your controller. – Dan Abramov Dec 10 '12 at 17:13
  • 1
    Which is as it should be according to the apple docs. UICollectionViewDelegateFlowLayout inherits UICollectionViewDelegate which implements those methods – svn Dec 11 '12 at 13:31
  • When I set the Delegate property in the constructor I get a null NavigationController property - moving the set operation to the ViewDidLoad() method of the controller does NOT produce this unwanted side effect. – Ian May 29 '14 at 19:32
  • Hi, Inside UiViewControll implementing Collectionview, could you share the code for how to implementment cell resize. thanks in advance. – RXGangam Aug 05 '15 at 01:42
9

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);
}
Marc
  • 11,341
  • 3
  • 27
  • 30
Iain Smith
  • 9,230
  • 4
  • 50
  • 61