OGeek|极客世界-中国程序员成长平台

标题: ios - 为什么 UICollectionViewFlowLayout 子类没有委托(delegate)属性? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:54
标题: ios - 为什么 UICollectionViewFlowLayout 子类没有委托(delegate)属性?

Apple 提供了 UICollectionViewFlowLayout 类,以及随之而来的 UICollectionViewDelegateFlowLayout 协议(protocol),以及类似

的方法

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat

我的理解是,默认情况下,UICollectionViewController 将自己设置为 UICollectionViewFlowLayout 的委托(delegate)。所以我假设 UICollectionViewFlowLayout 有一个要设置的委托(delegate)属性。但是,如果我将其子类化,则没有这样的事情。自动设置委托(delegate)的默认行为是否仍然适用?还是我只是误解了它的工作原理?



Best Answer-推荐答案


如果你注意你会发现 UICollectionViewDelegateFlowLayout 也是一个 UICollectionViewDelegate:

@protocol UICollectionViewDelegateFlowLayout <UICollectionViewDelegate>

所以 UICollectionView 委托(delegate)也是 UICollectionViewFlowLayout 委托(delegate)。我对这是如何实现的猜测是内部布局是这样的:

let flowDelegate = self.collectionView.delegate as? UICollectionViewDelegateFlowLayout
let minSpace = flowDelegate?.collectionView(self.collection, layout: self, minimumInteritemSpacingForSectionAtIndex: 0)

关于ios - 为什么 UICollectionViewFlowLayout 子类没有委托(delegate)属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39025035/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4