如何在UICollectionViewController中提供GetSizeForItem实现?

Dan*_*mov 7 xamarin.ios xamarin uicollectionview

UICollectionViewDelegateFlowLayout有一个名为sizeForItem的方法(MonoTouch中的GetSizeForItem).

但我没有明确地提供委托 - 相反,我继承了UICollectionViewController.
它混合了数据源和委托功能,但没有这种方法来覆盖.

我尝试将其添加到我的控制器:

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

它从未被称为.

如何在不诉诸分离委托和数据源的情况下提供此方法?

svn*_*svn 11

你不能.在Obj-C中,viewcontroller(或任何类)对象可以采用委托协议.这在Monotouch中是不可能的.您给了使用委托实例.但这可以是私人课程

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Iai*_*ith 8

编辑:无需创建委托子类,请在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);
}
Run Code Online (Sandbox Code Playgroud)