UICollectionViewLayout子类强制转换为子类

问题描述 投票:4回答:2

我试图抛出以下内容,但不断提出下面的错误。

我试图实现以下的目标c版本,工作正常:

LSSwipeToDeleteCollectionViewLayoutUICollectionViewFlowLayout的子类,用目标C写成。

目标C:

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

迅速:

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

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

swift
2个回答
2
投票

我反对这个问题时所做的是:

在viewDidLoad()中

需要添加以下代码以使用我们的新自定义流布局对象替换默认布局对象:

let carouselLayout = CarouselCollectionViewLayout()
self.collectionView?.setCollectionViewLayout(carouselLayout, animated: true)

设置集合视图:

let layout = self.collectionView!.collectionViewLayout as! CarouselCollectionViewLayout

ps:CarouselCollectionViewLayout是UICollectionViewFlowLayout的子类


0
投票

通常你会这样做:

let layout = self.collectionView.collectionViewLayout as! LSSwipeToDeleteCollectionViewLayout

您是否确定在初始化集合视图时首先创建了LSSwipeToDeleteCollectionViewLayout实例并将其设置为collectionViewLayout方法中的UICollectionView.init(frame:collectionViewLayout:)参数?

© www.soinside.com 2019 - 2024. All rights reserved.