Ios 为什么UICollectionView';s委托方法不';不行?
我创建了一个Ios 为什么UICollectionView';s委托方法不';不行?,ios,swift,uicollectionview,delegates,uicollectionviewdelegateflowlayout,Ios,Swift,Uicollectionview,Delegates,Uicollectionviewdelegateflowlayout,我创建了一个collectionView,并使其委托符合连接到主ViewController的UICollectionViewDelegateFlowLayout协议,以便修改UICollectionView的单元格的某些方面。 最初我是这样做的: override func viewDidLoad() { super.viewDidLoad() self.collectionView.delegate = self } 然后我意识到这不起作用,因为有一个错误,所以我通过这
collectionView
,并使其委托符合连接到主ViewController的UICollectionViewDelegateFlowLayout
协议,以便修改UICollectionView的
单元格的某些方面。
最初我是这样做的:
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.delegate = self
}
然后我意识到这不起作用,因为有一个错误,所以我通过这样做修复了它:
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.delegate = self as? UICollectionViewDelegateFlowLayout
}
问题是,我尝试在视图控制器中实现的每个方法在运行时似乎都不起作用
f、 e.我在这里实施了这种方法:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width/3, height: collectionView.frame.width/3)
}
但它在应用程序运行期间不起作用
“所以我就这么做了”
你让错误消失了,但你没有纠正它。您需要通过如下方式将视图控制器类添加到其类声明中,使其实际采用UICollectionViewDelegateFlowLayout
:
class ProfileViewController: UIViewController, UICollectionViewDelegateFlowLayout {
extension ProfileViewController: UICollectionViewDelegateFlowLayout {
或者将其添加到视图控制器的扩展中,如下所示:
class ProfileViewController: UIViewController, UICollectionViewDelegateFlowLayout {
extension ProfileViewController: UICollectionViewDelegateFlowLayout {