我想在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方法永远不会被触发。知道如何解决这个问题吗?