在C#中我们可以这样做:
public override UICollectionViewCell GetCell (UICollectionView collectionView, NSIndexPath indexPath)
{
var animalCell = (AnimalCell)collectionView.DequeueReusableCell (animalCellId, indexPath);
var animal = animals [indexPath.Row];
animalCell.Image = animal.Image;
return animalCell;
}
其中AnimalCell
是UICollectionViewCell
的子类。
在F#
中,我已将AnimalCell
定义为:
[<Register ("AnimalCell")>]
type AnimalCell (handle: IntPtr) =
inherit UICollectionViewCell (handle)
现在我正在实现相同的功能:
override x.GetCell(collectionView : UICollectionView, indexPath : NSIndexPath) =
let animalCell = (collectionView.DequeueReusableCell(new NSString("ChatCell"),indexPath)) :?> AnimalCell
let animal = animals.[indexPath.Row]
animalCell
这会产生错误:Error FS0001: This expression was expected to have type 'UICollectionViewCell' but here has type 'AnimalCell' (FS0001)
我可以通过以下方式解决这个问题:
animalCell :> UICollectionViewCell
但为什么这有必要?