如何将UICollectionViewCell SuperView获取为UICollectionView

时间:2019-05-18 14:17:47

标签: ios swift uicollectionview uicollectionviewlayout

我试图在CollectionViewCell类中获取UICollectionViewCell的indexPath。我为此创建了一个扩展,但返回nil

我尝试了这段代码

extension UICollectionViewCell{

    var collectionView:UICollectionView?{
        return superview as? UICollectionView
    }

    var indexPath:IndexPath?{
        return collectionView?.indexPath(for: self)
    }
}

它为superview as? UICollectionView返回nil

0 个答案:

没有答案