2016-08-15 81 views
2

我正在尝试投射以下内容,但不断提出下面的错误。UICollectionViewLayout子类强制转换为子类

我想达到以下,其正常工作的目标C版本:

LSSwipeToDeleteCollectionViewLayoutUICollectionViewFlowLayout子类,写在客观C.

目标C:

LSSwipeToDeleteCollectionViewLayout *layout = (LSSwipeToDeleteCollectionViewLayout *)self.collectionView.collectionViewLayout; 

Swift:

let layout : LSSwipeToDeleteCollectionViewLayout = (self.collectionView.collectionViewLayout as AnyObject) as! LSSwipeToDeleteCollectionViewLayout 
     layout.swipeToDeleteDelegate = self 

无法将类型'UICollectionViewFlowLayout' (0x111edda00)的值转换为'LSSwipeToDeleteCollectionViewLayout'(0x11040e3c0)。

回答

0

通常你会只是做:

let layout = self.collectionView.collectionViewLayout as! LSSwipeToDeleteCollectionViewLayout

你确定你的时候,你初始化第一次创建LSSwipeToDeleteCollectionViewLayout实例并将其设置为在UICollectionView.init(frame:collectionViewLayout:)方法collectionViewLayout参数集合视图?

+0

你好,你的意思是关于你的答案的第二部分? –

+0

当你创建一个集合视图时,你必须设置负责布局的类,它必须是'LSSwipeToDeleteCollectionViewLayout',否则你将得到默认的'UICollectionViewFlowLayout'类,这可能是你的类型转换失败的原因。根据创建集合视图的方式,您可以将布局类设置为在您的笔尖中,或者直接在代码中作为“UICollectionView”初始化程序的参数。 – par