GetSizeForItem不会触发

时间:2014-12-04 13:12:19

标签: xamarin

我想在UICollectionView中为每个单元格设置不同的高度。因此,我将覆盖UICollectionViewDelegateFlowLayout中的GetSizeForItem方法,如下所示:

public class MyCollectionViewDelegateFlowLayout : UICollectionViewDelegateFlowLayout
{
    public override CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
    {
        //  TODO custom height logic 
        return new SizeF(100, 100);
    }
}

这是我创建UICollectionView并使用自定义委托类的方式:

var flowLayout = new UICollectionViewFlowLayout()
        {
            ItemSize = new SizeF(320, 80),
            ScrollDirection = UICollectionViewScrollDirection.Vertical,
            MinimumLineSpacing = 0,
            MinimumInteritemSpacing = 0,
            SectionInset = new UIEdgeInsets(UIScreen.MainScreen.ApplicationFrame.Height - 100, 0, 0, 0)
        };

        var collectionView = new UICollectionView(UIScreen.MainScreen.ApplicationFrame, flowLayout);
        collectionView.Delegate = new MyCollectionViewDelegateFlowLayout();            

        collectionView.RegisterNibForCell(MyCollectionCell.Nib, MyCollectionCell.Key);
        var source = new MvxCollectionViewSource(collectionView, MyCollectionCell.Key);
        collectionView.Source = source;

问题是GetSizeForItem方法永远不会被触发。知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

来自Xamarin的论坛here,您似乎无法同时使用DelegateSource