Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么UICollectionView';s委托方法不';不行?_Ios_Swift_Uicollectionview_Delegates_Uicollectionviewdelegateflowlayout - Fatal编程技术网

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 {